15

I am writing an android project which has Native layer helping the java layer, and am stuck at a place where when i try to do a System.loadLibrary, it is throwing error that it is not able to link it.

I am using Target specific NDK to build the native layer, and then using ant to compile and create the apk.

On running on the device i get the following error.

Unable to dlopen(libsomething.so) Cannot load library: link_image[1995]: failed to link libsomething.so

The library get bundled into the apk, and is unpacked properly. If i try to remove the library manually and then run it, it then actually throws that library not found. So it is able to find the library, but it throws this error, and i am not able to find out why this error is coming.

Please help me.

Puneet
  • 565
  • 4
  • 9
  • 21

4 Answers4

11

First find the location of the .so file. and then can try:

Following example assumes the location of shared library as: /data/data/my.package/lib/libmysharedlibrary.so

try {
    //System.loadLibrary("mysharedlibrary");
    System.load("/data/data/my.package/lib/libmysharedlibrary.so");
} catch (UnsatisfiedLinkError use) {
    Log.e("JNI", "WARNING: Could not load libmysharedlibrary.so");
}
sulai
  • 5,204
  • 2
  • 29
  • 44
TheCottonSilk
  • 8,662
  • 2
  • 26
  • 37
  • Like I said, the library is present at the required location. I do get a log output saying that it is trying to load the library, but on actually trying to load, it gives me that error. However, will give this change a try as well.. Strangely, this same apk works on one device, but not on the other. And i totally confused on why this behavior is happening. – Puneet Jan 18 '11 at 12:14
  • Can you update on the path of the library file on both devices? – TheCottonSilk Jan 18 '11 at 12:34
  • It is packaged in apk, so it gets automatically unpacked while installing, to the same directory. i.e /data/data/package/lib/libsomething.so – Puneet Jan 18 '11 at 12:46
  • If the paths are correct on both devices, then its really strange. Can you paste the relevant part (exception details: 6/7 lines) of the logcat output. – TheCottonSilk Jan 18 '11 at 13:02
  • 11
    I found out the problem. My library was trying to link to some other shared library internally, and that other shared library was not present on the device throwing the link errors, but were present on the device that worked fine. :) It wasn't clear through Logcat why this was happening so was confused, but after doing some code comparision, and some trial-and-errors, it turned out to be this. Thanks for you help though. – Puneet Jan 18 '11 at 13:54
  • Good that you found your problem and could resolve. I think, if you had spent some time analyzing the logcat, you could have realized these things very earlier. Logcat very clearly tells what you have requested to link and whether it was able to find it or there was UnsatisfiedLinkError. – TheCottonSilk Jan 18 '11 at 16:58
10

I've trap in the same question. finally I got answer from this article: http://mpigulski.blogspot.com/2010/09/debugging-dlopen-unsatisfiedlinkerror.html

use

arm-linux-androideabi-readelf.exe  -d libs/armeabi/libmy.so

Then Found a NEEDED Shared library in wrong name.

musefan
  • 47,875
  • 21
  • 135
  • 185
g00g1e
  • 101
  • 1
  • 5
  • I'm fighting what appears to be the same problem. I have 6 listed under NEEDED, but how can you tell what is present in the device? Some devices work, some don't. – SpacemanScott May 17 '18 at 12:59
5

Sometimes (most of the time!) you library requires other libraries as mentioned by @musefan. and you can list them doing readelf -d libs/armeabi/libmy.so. However there is a catch here: since Android has no any mechanism to control library version (like in normal linux you have liblzma.so.1, liblzma.so.2, etc) the library you need is there (liblzma.so) BUT has no some symbols imported by your library. Here is the live example: you use android::ZipFileRO::getEntryInfo function located in libutils.so. All version of the library has this function however the PROTOTYPE of the function has been changed at the end of 2010 so you app built for 4.0.4 NDK will not run on FroYo devices or GB devices with the same symptomatic: dlopen cannot load library. Here is the recipe how to detect such cases: you need the contents os /system/lib folder on your PC. It may be folder dumped from your device if you are 3rd party App developed or built if you are platform developer. then issue the command arm-linux-gnueabi-ld -rpath-link /path/to/system/lib ./lib_mylib.so and you will see something lie this in case of error
lib_mylib.so: undefined reference toandroid::ZipFileRO::getEntryInfo(void*, int*, long*, long*, long*, long*, long*) const'`

mishmashru
  • 459
  • 5
  • 8
3

You can also find this error while using calling System.load(String pathName) method and just passing libraryName instead of complete path to library.

Resolution : use System.loadLibrary(String libName) method and now pass the libraryName.

Napolean
  • 5,303
  • 2
  • 29
  • 35