java.lang.UnsatisfiedLinkError – android studio gradle中的NDK?

文件夹结构

app ---main ---java ----jni -----Android.mk -----Application.mk ----- hello-jni.c ---res 

在build.gradle

 apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "22.0.1" defaultConfig { applicationId "com.example.hellojni" minSdkVersion 17 targetSdkVersion 21 sourceSets.main { jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' } ndk { moduleName "hello-jni" cFlags "-std=c++11 -fexceptions" ldLibs "log" stl "gnustl_shared" abiFilter "armeabi-v7a" } task nativeLibsToJar(type: Zip, description: 'create a jar archive of the native libs') { destinationDir file("$buildDir/native-libs") baseName 'native-libs' extension 'jar' from fileTree(dir: 'libs', include: '**/*.so') into 'lib/' } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn(nativeLibsToJar) } testApplicationId "com.example.hellojni.tests" testInstrumentationRunner "android.test.InstrumentationTestRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } dependencies { compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar') } } 

在Android.mk中

  LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hello-jni LOCAL_SRC_FILES := hello-jni.c include $(BUILD_SHARED_LIBRARY) 

在hello-jni.c中

 include #include  #include  jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { #if defined(__arm__) #if defined(__ARM_ARCH_7A__) #if defined(__ARM_NEON__) #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a/NEON (hard-float)" #else #define ABI "armeabi-v7a/NEON" #endif #else #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a (hard-float)" #else #define ABI "armeabi-v7a" #endif #endif #else #define ABI "armeabi" #endif #elif defined(__i386__) #define ABI "x86" #elif defined(__x86_64__) #define ABI "x86_64" #elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */ #define ABI "mips64" #elif defined(__mips__) #define ABI "mips" #elif defined(__aarch64__) #define ABI "arm64-v8a" #else #define ABI "unknown" #endif return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI "."); } 

在java中

  public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); } public native String stringFromJNI(); public native String unimplementedStringFromJNI(); static { System.loadLibrary("hello-jni"); } } 

我得到了不满意的错误

  java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.hellojni-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libhello-jni.so" at java.lang.Runtime.loadLibrary(Runtime.java:366) at java.lang.System.loadLibrary(System.java:989) at com.example.hellojni.HelloJni.(HelloJni.java:64) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1572) at android.app.Instrumentation.newActivity(Instrumentation.java:1088) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

我的local.properties

 sdk.dir=F\:\\SDK ndk.dir=C\:\\NDK 

它不生成.so文件..

你的build.gradle中有几个错误:

nativeLibsToJar任务是gradle android插件的早期版本中的相关技术,它不应该使用至少0.7,它可能导致错误,删除此任务。 相反,如果你将.so文件放在jniLibs/ABI目录(其中ABI是armeabi-v7a,x86 ……)中,它们将被正确地集成到你的APK中。

然后,你设置:

 jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' 

它从gradle插件中取消激活内置的ndk集成,并使gradle使用libs而不是jniLibs libs 。 那很好,但是后来你用ndk {}块来定义你的ndk模块,然后删除它。

清理完这些部件后,您可以从Android项目的根目录中从命令行调用ndk-build (如果您使用的是Windows,则调用ndk-build ndk-build.cmd )。 它将在libs/ABI生成你的.so文件,它们将集成到你的APK中,因为jniLibs.srcDir 'src/main/libs'告诉gradle从libs获取这些libs

您可以通过将APK作为zip文件打开来检查您的.so文件是否已集成; 您的.so文件必须存在于lib/ABI