android jni通过cmake使用第三方静态库和动态库

google 官方现在推荐使用cmake来构建jni。


本人正好工作需要使用第三方的静态库和动态库,写此文 权当做个记录。


首先修改app的build.gradle文件 


ndk {
    abiFilter 'armeabi' }
externalNativeBuild {
    cmake {
        arguments '-DANDROID_PLATFORM=android-13',
                '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'  }
}


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文件,最终结果见下图
 
 
 
 

你可能感兴趣的:(android jni通过cmake使用第三方静态库和动态库)