如果在建立完CMakeLists.txt後,C++檔案已經準備就緒,但是你發現無論怎麼build或者是run這個項目,當你在類中System.loadLibrary("xxxx-lib");時,就是一直報錯,
java.lang.UnsatisfiedLinkError :dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.houde.jni-2/base.apk"],nativeLibraryDirectories=[/data/app/com.houde.jni-2/lib/arm64, /data/app/com.houde.jni-2/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]] couldn't find "libnative-lib.so"
說找不到這個so檔案,當你去build檔案夾下面去找這個so的時候,确實沒有生成,這是因為這個CMakeLists.txt檔案沒有在build.gradle中關聯,在build的時候不會進行編譯C++檔案的操作,可以使用下文的方法進行配置:
方法一:
方法二:
手動配置Gradle
android {
...
defaultConfig {...}
buildTypes {...}
// Encapsulates your external native build configurations.
externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "CMakeLists.txt"
}
}
}
其實這兩種方法是一樣的,方法一也是在build.gradle中增加一個externalNativeBuild.