externalNativeBuild {cmake {path 'src/main/cpp/CMakeLists.txt'
}
}sourceSets {main {// copy libusb1.0.so to APK
jniLibs.srcDirs = ['/home/chenhf/libusb/android/libs']}
}
創建CMakeLists.txt文件
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.cmake_minimum_required(VERSION 3.4.1)# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.set(LIBHARDWARE_DIR /home/chenhf/workspace/android/PSAMDemo/app/libhardware)
add_library(hardware STATIC IMPORTED)
set_target_properties(hardware PROPERTIES IMPORTED_LOCATION ${LIBHARDWARE_DIR}/${ANDROID_ABI}/libhardware.a)set(LIBUSB_DIR /home/chenhf/libusb/android/libs)
add_library(usb SHARED IMPORTED)
set_target_properties(usb PROPERTIES IMPORTED_LOCATION ${LIBUSB_DIR}/${ANDROID_ABI}/libusb1.0.so)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")add_library(psamwrapper SHARED psamwrapper.cpp)
target_link_libraries(psamwrapper hardware usb android log)最后會生成so文件,最終結果見下圖