Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null

Android NdkJava Native-Interface

Android Ndk Problem Overview


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?

Android Ndk Solutions


Solution 1 - Android Ndk

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

System.loadLibrary("XYZ");

Solution 2 - Android Ndk

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.

Solution 3 - Android Ndk

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.

Solution 4 - Android Ndk

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.

Solution 5 - Android Ndk

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.

Solution 6 - Android Ndk

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.

Solution 7 - Android Ndk

First, check you have the jni files inside you libs folder in eclipse or jniLibs folder in android studio. When you checkin in any svn, cross check to check in the .so files too.

My Scenario was when Building with Android studio.

When you are building in android studio, your library .so files or (jni) files should be inside the \src\main\jniLibs\armeabi***.so folder.

where as these files will be inside the \libs\armeabi***.so in eclipse.

When building using both eclipse and android studio, you have to modify your build.gradle file as

main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']

            // Fixed Issue : "android studio java.lang.unsatisfiedlinkerror: couldn't load find library returned null"
            // Cause : For android studio : The JNI files should be placed in the /src/main/jniLibs folder.
            // Fix : Mapping the jniLibs 's source Directories with Lib's folder
            jniLibs.srcDirs = ['libs']
}

Here I am building the project using both the android studio and the eclipse.

Solution 8 - Android Ndk

just had a similar problem.

Check out your /data/data/your.package.name/lib directory

When i ls in my package directory it currently displays:

lib -> /mismatched_uid/settings_10037/fs_1000

probably i accidently switched the sharedUserId and thus the library can't be accessed anymore.

Solution 9 - Android Ndk

None of the previous answers solved my problem, but this did: All along the problem was that a necessary subdirectory and file were not present. All I had in my libs folder was an armeabi folder containing the proper .so file, but there are supposed to be 3 others, each with a .so file in them. Not certain yet which of the other three (armeabi-v7a, mips, or x86) was the required one, but I do know that all three were automatically generated when I added the Application.mk file to the same folder as the Android.mk file, and made sure it had the following line in it:

APP_ABI := all

For me, that line is the only text in there. When ndk-build is then run the Application.mk file apparently causes "all" the 4 folders to be created and the proper .so files to be created in them. Once I got Application.mk in place, I ran ndk-build again, and then did a clean and a clear on my Eclipse project before trying again. Everything ran perfectly.

Solution 10 - Android Ndk

No need to root the device..deploy app on emulator and browse the folder

Solution 11 - Android Ndk

If you have a native project with "libXYZ.so", make sure that /system/lib/libXYZ.so does not exist on your device. There is a painful workaround: use

System.load("/data/data/your.package.name/lib/libXY.so") 

instead of System.loadLibrary().

Solution 12 - Android Ndk

I just had this happen and the problem was that the device simply did not have enough space to install the library. I uninstalled some other apps and then it worked.

Solution 13 - Android Ndk

Had identical problem. Just cleaned project and it worked. Mystery..

Solution 14 - Android Ndk

UnsatisfiedLinkerror is solved by this.Build your .so file again by "ndk_build"

Solution 15 - Android Ndk

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

Solution 16 - Android Ndk

Add this following code inside your gradle file

//libs is the location of your library's folder, It varies in diffarent projects like src/main/libs etc.
android {
  sourceSets.main {
    jniLibs.srcDir 'libs'
  }
}

So, Android Studio keep on looking native libs inside jniLibs folder. this code will change the path of android studio to look libs in libs folder

Solution 17 - Android Ndk

In my case .so library is loaded correctly, but can not find method like below:

> Caused by: java.lang.UnsatisfiedLinkError: Native method not found...

After hours of searching what can be problem noticed that application throws related error only for production flavor. Yes, we use proguard to obfuscate source code and it breaks .so library handler class... After adding below lines into proguard rules file. It started working as expected.

-keepclasseswithmembers,allowshrinking,allowoptimization class com.zk.android.jni.Device {
native <methods>; }

So if you use proguard and having similar problem with mine. Please check proguard file...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLee DixonView Question on Stackoverflow
Solution 1 - Android NdkRoyView Answer on Stackoverflow
Solution 2 - Android NdkMikeView Answer on Stackoverflow
Solution 3 - Android NdkBill HooView Answer on Stackoverflow
Solution 4 - Android Ndkmn0102View Answer on Stackoverflow
Solution 5 - Android NdksteveloView Answer on Stackoverflow
Solution 6 - Android NdkDanielView Answer on Stackoverflow
Solution 7 - Android NdkVishnu PrabhuView Answer on Stackoverflow
Solution 8 - Android NdkicyerasorView Answer on Stackoverflow
Solution 9 - Android NdkAlyoshakView Answer on Stackoverflow
Solution 10 - Android NdkMayank MehtaView Answer on Stackoverflow
Solution 11 - Android NdkAlex CohnView Answer on Stackoverflow
Solution 12 - Android NdkKeith JohnstonView Answer on Stackoverflow
Solution 13 - Android NdkDrag0View Answer on Stackoverflow
Solution 14 - Android NdkUdit KumawatView Answer on Stackoverflow
Solution 15 - Android NdkTrần Thị Diệu MyView Answer on Stackoverflow
Solution 16 - Android NdkHarish ReddyView Answer on Stackoverflow
Solution 17 - Android NdkAlper ÖzaslanView Answer on Stackoverflow