Android NDK java.lang.UnsatisfiedLinkError: findLi

2020-01-27 02:29发布

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?

16条回答
贼婆χ
2楼-- · 2020-01-27 02:49

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.
查看更多
疯言疯语
3楼-- · 2020-01-27 02:51

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

查看更多
虎瘦雄心在
4楼-- · 2020-01-27 02:52

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.

查看更多
Fickle 薄情
5楼-- · 2020-01-27 02:57

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.

查看更多
Fickle 薄情
6楼-- · 2020-01-27 02:57

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

查看更多
Melony?
7楼-- · 2020-01-27 02:59

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

查看更多
登录 后发表回答