Android Ndk Java.lang.unsatisfiedlinkerror On Sample Hello-jni
Solution 1:
This is because Android Studio ignore your Android.mk and generate own one on-the-fly, taking settings from gradle script. So you should either specify correct settings in gradle script (which works only in simplest cases due to incomplete NDK support in Android Studio), or completely disable Android Studio's limited NDK support and call ndk-build directly from gradle.
To specify NDK-related settings in gradle script, add the following section:
defaultConfig {
...
ndk {
moduleName "my-module-name"
cFlags "-std=c++11 -fexceptions"
ldLibs "log"
stl "gnustl_shared"
abiFilter "armeabi-v7a"
}
}
And set your project specific values there. Android Studio's gradle plugin then will take those settings into account and auto-generate Android.mk on-the-fly with these settings.
Note, however, that the only available NDK options are those listed above. If you have more complicated NDK setup (for example, several NDK modules, depending each on other, or you want use prebuilt libraries, or whatever else), you should completely disable Android Studio's limited NDK support and call ndk-build directly from gradle. To do that, first disable auto-generation of Android.mk:
android {
.....
// disable automatic ndk-build call, which ignore our Android.mk
sourceSets.main.jni.srcDirs = []
}
Then add the following lines:
android {
.....
sourceSets.main.jniLibs.srcDir'src/main/libs'// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd()
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
task cleanNative(type: Exec) {
workingDir file('src/main')
commandLine getNdkBuildCmd(), 'clean'
}
clean.dependsOn cleanNative
}
Finally, add helper functions to the end of gradle script:
def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
return System.env.ANDROID_NDK_ROOT
Propertiesproperties=newProperties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
defndkdir= properties.getProperty('ndk.dir', null)
if (ndkdir == null)
thrownewGradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")
return ndkdir
}
def getNdkBuildCmd() {
defndkbuild= getNdkDir() + "/ndk-build"if (Os.isFamily(Os.FAMILY_WINDOWS))
ndkbuild += ".cmd"return ndkbuild
}
Ah, and don't forget to add "import" to the beginning of the gradle script:
import org.apache.tools.ant.taskdefs.condition.Os
Here I've described it more detailed.
Post a Comment for "Android Ndk Java.lang.unsatisfiedlinkerror On Sample Hello-jni"