Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null

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 build system (perhaps regarding Eclipse) is broke, and I hope to save someone else hours of pain. Perhaps others have come across this issue and can comment on what worked for them.

For a while, I've had an Android project with some JNI code that I developed using the NDK. Then, today, I changed something in the java code and then poof, I could no longer load my JNI library. It failed with an exception like:

E/AndroidRuntime( 999): java.lang.UnsatisfiedLinkError: Couldn't load mylibrary: findLibrary returned null

I googled and tried everything (rebuilding, close and relaunch Eclipse, etc, etc)

What finally fixed my problem? I physically uninstalled my app from the device before trying another run. That's it. After that, it worked. What worked for you?

like image 507
Lee Dixon Avatar asked Jan 27 '12 18:01

Lee Dixon


6 Answers

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

System.loadLibrary("XYZ");
like image 174
Roy Avatar answered Oct 31 '22 21:10

Roy


If you are trying to run your app in a simulator, then make sure that you have specified the correct architecture in Run -> Run Configurations -> Target (you may need to add the required simulator using Window -> Android Virtual Device Manager).

I had the same problem when trying to execute an app in an Intel simulator, while the app was using a library precompiled for the ARM.

like image 36
Mike Avatar answered Oct 31 '22 22:10

Mike


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.
like image 11
Bill Hoo Avatar answered Oct 31 '22 21:10

Bill Hoo


In my case, while making a System Application the issue was related to permissions. After putting the ".so" files into /system/lib/ or /system/vendor/lib/ directory, I modified the default allocated permissions 600 to 755. It worked well.

like image 7
aditya Avatar answered Oct 31 '22 22:10

aditya


Inside libs folder, I create a new folder called armeabi-v7a, and copied the .so file from armeabi to the new folder. It solves the error.

like image 6
stevelo Avatar answered Oct 31 '22 21:10

stevelo


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.

like image 5
Daniel Avatar answered Oct 31 '22 23:10

Daniel