Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null

后端 未结 16 513
我在风中等你
我在风中等你 2020-11-30 01:56

Having the above error in your Android JNI app? Read on...

Up front, I\'ll say that I\'ve already solved this, in my own way, but I feel something in the Android bu

相关标签:
16条回答
  • 2020-11-30 02:22

    I was having the same issue and these are some of the problems I had with my project:


    1. Changing from System.load("hello-test"); into System.loadLibrary("hello-test");;
    2. Changing the C function signature to JNIEXPORT jstring Java_com_example_testndk_TestNDK_funcNami(JNIEnv* env, jobject thiz): here you use Java_<java_package_name>_<java-class-name>_<function-name>;
    3. Adding NDK path to local.properties, in my case ndk.dir=<my-path-to-ndk-directory>; and
    4. Updating my build.gradle to also include within defaultConfig: ndk { moduleName "hello-test" }.

    • Package name: com.example.testndk
    • Java class name: TestNDK
    • C source name: hello-test.c
    • JNI directory location within the project structure: AndroidProjects/TestNDK/app/src/main/jni

    IDE: Android Studio 0.8.1.

    0 讨论(0)
  • 2020-11-30 02:24

    I added

    extern "C"
    

    before functions declare
    ex:

    extern "C"
    jstring
    Java_lara_myapplication_MainActivity_stringFromJNI(
                JNIEnv *env,
        jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    

    then the error was gone

    0 讨论(0)
  • 2020-11-30 02:25

    I've got this issue too, but for my situation, i'm using my libs in another project. And I don't think it's a matter of eclipse or android-ndk.

    you may check these tips that can lead to UnsatisfiedLinkError and it works fine for me, good luck :)

    1. must following the JNI interface's nameing rule(you said that you did load the library successfully before, so you can ignore this) and the libraries must in directory /your project/libs/armeabi/
    2. if you ensure the above things are done, then you must ensure that your generated libs are installed into your app.apk, otherwise it will still cannot find the lib and thorw you an error. to do this, you right click on your eclipse project name, and go into Build Path - Configure Build Path, on the left menu, choose Java Build Path, then click on the Order and Export tab on the right panel, checking the checkbox before your library name and click OK, then clean,rebuild and run you project, and your libs will be installed into app.apk, things are done.

    enter image description here

    enter image description here

    enter image description here

    EDIT:

    1. in a word, UnsatisfiedLinkError are thrown if the library is not installed into your app.apk, so it cannot be linked sucessfully.
    0 讨论(0)
  • 2020-11-30 02:26

    If you have a native project with LOCAL_MODULE "libXYZ", make sure to load it as

    System.loadLibrary("XYZ");
    
    0 讨论(0)
提交回复
热议问题