java.lang.UnsatisfiedLinkError中 - NDK在android系统的gradle工作室?工作室、系统、UnsatisfiedLinkError、lang

由网友(你真社会)分享简介:文件夹结构app ---main---java----jni-----Android.mk-----Application.mk----- hello-jni.c---res在的build.gradle in build.gradleapply plugin: 'com.android.application'a...

文件夹结构

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

在的build.gradle

in 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

in 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

in hello-jni.c

include<com.example.hellojni.HelloJni.h>

#include <string.h>
 #include <jni.h>



 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");
}
}

我越来越不满意误差

I am getting unsatisfied error

 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.<clinit>(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

My local.properties

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

它不产生.so文件。

Its not generating .so files ..

推荐答案

您已经有了你的build.gradle几个错误:

You've got several mistakes in your build.gradle:

nativeLibsToJar 的任务是在gradle这个Android插件的早期版本相关的技术,它不应该因为至少0.7使用,它可能会导致错误,删除此任务。相反,如果你把你的.so文件里面 jniLibs / ABI 目录(其中ABI是armeabi-V7A,86 ...),他们将获得适当整合您的APK里面。

The nativeLibsToJar task was a relevant technique in early versions of the gradle android plugin, It shouldn't be used since at least 0.7 and it may lead to bugs, remove this task. Instead, if you put your .so files inside jniLibs/ABI directories (where ABI is armeabi-v7a, x86...), they will get properly integrated inside your APK.

然后,您已设置:

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

据停用内置NDK集成从gradle这个插件,并gradle这个使用库从,而不是从 jniLibs 。这很好,但后来你与 NDK {} 块,这是没有用的,然后定义你的NDK模块,将其删除。

It deactivates the built-in ndk integration from the gradle plugin and make gradle use libs from libs instead of from jniLibs. That's fine, but later you're defining your ndk module with a ndk {} block, which is useless then, remove it.

一旦你清理这些部位,你可以叫 NDK的构建(或 NDK-build.cmd 如果你使用的是Windows)命令行,从Android项目的根目录。它会生成库里面的.so文件/ ABI ,自 jniLibs.srcDir'的src / main /库,他们将获得集成到您的APK 的gradle告诉摆脱库这些库

Once you've cleaned these parts, you can call ndk-build (or ndk-build.cmd if you're using Windows) from command line, from the root of your Android project. It will generate your .so files inside libs/ABI and they will get integrated into your APK since jniLibs.srcDir 'src/main/libs' told gradle to get these libs from libs.

您可以检查您的.so文件获得通过打开您的APK作为一个zip文件集成;你的.so文件必须在的lib / ABI

You can check that your .so files get integrated by opening your APK as a zip file; Your .so files have to be present under lib/ABI.

阅读全文

相关推荐

最新文章