53

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?

LU RD
  • 34,438
  • 5
  • 88
  • 296
Lee Dixon
  • 839
  • 1
  • 7
  • 8
  • I've seen odd behavior where I had to copy the .so file out of the \LIBS\ARMEABI directory and place it in the \LIBS directory for it to find it. – BitBank Jan 27 '12 at 20:55
  • 1
    I have the same problem with my app, unfortunately some of my customers receive this problem (I see lots of crash reports for this). I don't know why it happens only on "some", but it works on most. I was never able to reproduce it myself too. Very strange... – Ertan D. May 17 '12 at 07:25
  • I had two instances of my app on the device, having just added a sharedUserId to the manifest, and forgot to uninstall the old version first. Thanks for the reminder! – Alan Feb 08 '13 at 01:58
  • 1
    @BitBank You nailed it! My app worked well on the emulator , but not on the phone, I copied the libraries to all the Intel, amreabi folders and it worked. Hell yes. – Josh Sep 01 '14 at 09:51
  • http://support.crashlytics.com/knowledgebase/articles/605193-unsatisfiedlinkerror-exception-for-ndk – TheIT Jul 06 '15 at 16:35

17 Answers17

15

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

System.loadLibrary("XYZ");
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Roy
  • 673
  • 11
  • 21
13

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.

Mike
  • 171
  • 1
  • 3
11

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.
Bill Hoo
  • 647
  • 5
  • 21
7

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.

mn0102
  • 839
  • 1
  • 12
  • 25
  • 1
    While the issue is real, your settings are incorrect. You are missing the execute bit, so this won't work on ARM systems which enforce that. Normal .so file permissions on Android are 755. – Chris Stratton Aug 07 '14 at 14:39
  • @ChrisStratton thanks for correcting. I dint mention it but I used 755, default were 600 on one of my lab's android devices. – mn0102 Aug 08 '14 at 11:39
  • @aditya I have similar requirement where i have to make a system application. I did even change the permission to 777 yet I am getting the same error, Do you have any clue about it? – darthvading Jun 02 '15 at 11:29
6

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.

stevelo
  • 114
  • 1
  • 2
5

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.

Daniel
  • 2,380
  • 29
  • 44
4

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.

Vishnu Prabhu
  • 449
  • 1
  • 5
  • 19
2

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.

icyerasor
  • 4,973
  • 1
  • 43
  • 52
  • 3
    could you clarify on this a bit? I'm getting the same error and what you just said is pretty far over my head – JuiCe Jul 31 '12 at 16:28
  • In my case i changed the sharedUiserId=".." in my AndroidManifest. Thus the application is run with a different user, but the folders/files still belong to another user. When rooted you can navigate into your /lib directory and use the "ls" command to show who the files belong to. – icyerasor Aug 04 '12 at 16:05
  • You can `ls -l /data/data/your.package.name/lib` on any phone, you may not have access to its parent, though. So, you need a rooted device to _navigate_ to this directory. – Alex Cohn Nov 20 '12 at 06:36
  • Yep, that's right Alex. Not sure if it works in this case though, as the lib dir itself seems to have changed permissions - but certainly worth a try when not rooted. Modified the answer a bit. – icyerasor Nov 20 '12 at 13:04
2

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.

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
1

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

Mayank Mehta
  • 689
  • 1
  • 8
  • 12
1

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

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
1

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.

Keith Johnston
  • 2,151
  • 1
  • 13
  • 14
0

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

Drag0
  • 8,438
  • 9
  • 40
  • 52
0

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

Udit Kumawat
  • 654
  • 1
  • 8
  • 21
0

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

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

Harish Reddy
  • 922
  • 10
  • 20
0

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

Alper Özaslan
  • 271
  • 2
  • 12