我们知道当我们编译自己的共享库时,会首先将共享库清除,如果此时我们我们有第三方库也会跟着被清除,这个时候我们是不会想清除第三方库的,这个时候我们就需要将自定义库和第三方库的加载路径设置为不同的路径,那么我们现在看看如何设置吧
设置第三方加载库和自定义库路径
1.1 设置自定义库加载路径
CMakeList.txt:
......
#此目录为CMakeList.txt 的父目录下的jniLibs目录
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})
......
1.2 设置第三方库加载路径
在模块的build.gradle中设置第三方库的加载路径:
在gradle plugln 4.0开始,无需指定第三库加载路径,gradle自动会帮助我们寻找,否则会出现以下错误:
Execution failed for task ‘:app:mergeDebugNativeLibs’.
A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
More than one file was found with OS independent path ‘lib/arm64-v8a/lib***.so’. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake
google给出的官方说明:
build.gradle:
......
android {
......
defaultConfig {
......
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a','arm64-v8a'
}
sourceSets.main.jniLibs.srcDirs = ['libs']
......
}
......
}
......