debugging JNI Library loading and UnsatisfiedLinkError Issues

842 views
Skip to first unread message

TA

unread,
Dec 8, 2009, 4:48:51 PM12/8/09
to android-ndk
Recently I faced an issue where my native library (JNI Lib) was
throwing java.lang.UnsatisfiedLinkError exception and logcat logs were
not helpful in rootcausing the issue. My library was working just fine
on several devices but on on a particular device, I will get
UnsatisfiedLinkError exception. On Android-NDK group, I noticed few
mentions of using strace and potentially the problem being associated
with poor error escalation of android linker (.../bionic/linker/
linker.c) in Android JNI. However, it took me a bit more time to
finally reach the root cause. I thought I will share these steps and
my experience here for the benefit of others.

It turns out that my problem was related to pre-linking of my library.
However, to figure out this problem, I used following steps. I hope
these steps help you as well.

1. Add a sleep(<a long time val, such as 10 secs or so>) before your
System.loadLibrary(<ur lib name>); call in your program.
2. Start your Android program and using ps command find PID of your
App.
3. Start strace with following parameters.
strace -v -s 256 -p <ur program's PID>

Once your sleep time in your program expires, you will notice detailed
output in strace and hopefully you will be able to see the exact root-
cause of why system was unable to load your Native Lib.

In my case, I was able to see that the error was coming from linker.c
( and get the line number too ) and it was due to the linker unmapping
my lib because it could not be loaded at the address requested at
prelinking stage.

kcr

unread,
Dec 16, 2009, 10:16:03 AM12/16/09
to android-ndk
Hi,

This is a very useful information. I am also debugging a similar
issue. I have a JNI call from an Activity and it works fine and I can
display the string that is returned by my JNI code to activity which
then prints on screen. This is the basic hello-jni sample app from
NDK. I have tried to move the System.loadLibrary call to a service
class in the onStart(), along with the nativa function definitions but
I get an app error and log cat messages are as below. main issue
UnsatisfiedLinkError. I am trying this on donut release. I will try
the debugging methods you suggested but was wondering if you had a
quick tip on this ?

12-16 15:57:28.454: WARN/dalvikvm(909): threadid=3: thread exiting
with uncaught exception (group=0x4001aa28)
12-16 15:57:28.454: ERROR/AndroidRuntime(909): Uncaught handler:
thread main exiting due to uncaught exception
12-16 15:57:28.474: ERROR/AndroidRuntime(909):
java.lang.UnsatisfiedLinkError: stringFromJNI
12-16 15:57:28.474: ERROR/AndroidRuntime(909): at
com.example.hellojni.ServiceApp.stringFromJNI(Native Method)
12-16 15:57:28.474: ERROR/AndroidRuntime(909): at
com.example.hellojni.ServiceApp.onStart(ServiceApp.java:69)
12-16 15:57:28.474: ERROR/AndroidRuntime(909): at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2776)
12-16 15:57:28.474: ERROR/AndroidRuntime(909): at
android.app.ActivityThread.access$3400(ActivityThread.java:116)

Regards,
kcr
Reply all
Reply to author
Forward
0 new messages