我已经使用ndk独立工具链(standalone toolchain)编译了第三方库,libabc.so。需要在android studio 中封装jni接口,对.so库函数进行调用。
1,修改当前工程CMakeList.txt,添加依赖的库和头文件:
include_directories(${PROJECT_SOURCE_DIR}/libs/include)
target_link_libraries(# Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
${PROJECT_SOURCE_DIR}/libs/armeabi-v7a/libabc.so
${PROJECT_SOURCE_DIR}/libs/armeabi-v7a/libdependecy1.so
${PROJECT_SOURCE_DIR}/libs/armeabi-v7a/libdependecy2.so
${PROJECT_SOURCE_DIR}/libs/armeabi-v7a/libdependecy3.so
${PROJECT_SOURCE_DIR}/libs/armeabi-v7a/libdependecy4.so)
2,修改app的build.gradle文件,添加如下字段,依赖库文件夹和依赖库平台:
sourceSets{
main{
jniLibs.srcDirs = ['libs']
}
}
flavorDimensions'cpuArch'
productFlavors {
arm7 {
dimension'cpuArch'
ndk {
abiFilter'armeabi-v7a'
}
}
universal {
dimension'cpuArch'
// include all default ABIs. with NDK-r16, it is:
// armeabi-v7a, arm64-v8a, x86, x86_64
}
}
3,在app文件夹下新建libs文件夹,并将依赖库放入对应平台目录中,如下图所示:
4,建立java和cpp文件,交互数据,cpp中包含库的头文件,在cpp中能够直接调用库函数了。
网友评论