0

I'm using VS-Android to build my NDK program. I have two libraries, "bridge" and "utils", and the first one uses the second one. Both libraries compile succesfully and the files are named correctly as libbridge.so and libutils.so. However, when I try to load the files, I get an UnsatisfiedLinkError saying that libbridge.so cannot access liblibutils. Why is it trying to search for liblibutils?

Here's the Java code I use to load the libraries:

static
{
    System.loadLibrary("utils"); // This loads OK
    System.loadLibrary("bridge"); // This is found but causes the UnsatisfiedLinkError
}

The exact error I get is:

java.lang.UnsatisfiedLinkError: dlopen failed: could not load library "liblibutils"
needed by "libbridge.so"; caused by library "liblibutils" not found

Both library files are located in the same folder:

(projectfolder)/libs/armeabi-v7a/libbridge.so
(projectfolder)/libs/armeabi-v7a/libutils.so

Now, in Visual Studio, the relevant output of the linking phase of the libutils.so is this:

-o E:/NDKProject/Debug/armeabi-v7a/libutils.so -lc -lm -llog -lgcc -Wl,-soname,libutils

And the relevant output of the libbridge.so:

-o E:/NDKProject/Debug/armeabi-v7a/libbridge.so
-LE:/NDKProject/Debug/armeabi-v7a -lutils -lc -lm -llog -lgcc -Wl,-soname,libbridge

The project properties for the libutils.so is like this:

  • Target name: libutils
  • 'soname' Setting: $(TargetName)

And for the libbridge.so:

  • Target name: libbridge
  • 'soname' Setting: $(TargetName)
  • Additional Dependencies: -lutils

I'm baffled. Where does the extra "lib" come when the bridge library tries to use the utils library? The utils library does load succesfully, and so does the bridge library if I don't link the utils library and comment out all the calls to the stuff in the utils library.

So far, I've tried to change the target name to "utils" instead of "libutils", but it doesn't work. I've also tried to use lib$(TargetName) as the soname setting, but it doesn't work either.

manabreak
  • 5,415
  • 7
  • 39
  • 96

2 Answers2

0

I don't know why the log mentions liblibutils, but your code should be changed if you want it to work on typical Android system. The reason is that libutils.so is part of a standard distribution, and sits in /system/lib. For loadLibrary() and for the native loader, /system/lib takes precedence over the app-specific lib directory, and your libutils.so never had the chance to load. Hence, UnsatisfiedLinkError is inevitable.

Use a more unique name for your utils library, and loader well be happy.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Does it still complain about **liblibutils.so**? – Alex Cohn Oct 11 '14 at 17:54
  • I would suggest to try target name `myownutils` and soname for it `libmyownutils.so` (with **lib** prefix and **.so** suffix). – Alex Cohn Oct 12 '14 at 06:59
  • Tried, and still it complains about liblibmyownutils missing. I don't think the problem is in the utility library at all, since it loads just fine. I suspect the problem lies in the bridge library, as it is the one causing the error. – manabreak Oct 12 '14 at 07:35
  • Did you adjust the Java code to `loadLibrary("myownutils")`? – Alex Cohn Oct 12 '14 at 12:26
  • Yeah, I changed the call. – manabreak Oct 12 '14 at 13:41
  • You are right, there is something wrong with how libmyownutils is linked. To better understand what happens here, you could try to keep the soname as is, but rename the target file to be `libmyownu.so`. Let's see if the error correlates with the file name or with the **soname**. – Alex Cohn Oct 12 '14 at 14:17
0

I found the cause of the problem. Turned out that I was linking to a wrong file (wrong folder) where an old version of the utility library was located.

manabreak
  • 5,415
  • 7
  • 39
  • 96