Using shared library that is dependent on other NDK shared library

818 views
Skip to first unread message

Yadnesh

unread,
Jul 15, 2010, 9:45:10 AM7/15/10
to android-ndk
Hi,

I am modifying the "hello-jni" sample to call function from another
shared library that I am using with android. The library libmyadd.so
gets compiled. I also verified that it is copied to correct location
on the emulator. But when I launch HelloJni application from eclipse,
I get error "could not load needed library 'libmyadd.so' for 'libhello-
jni.so' (load_library[984]: Library 'libmyadd.so' not found)"

Is there something wrong with "LOCAL_SHARED_LIBRARIES" that I am
using? Do I need to specify the library path in some way? Please
help.

All suggestions welcome :)

The "hello-jni"/jni folder structure now looks like:
============================================================================================
-rwxrwxrwx+ Android.mk
-rwxrwxrwx+ Application.mk
-rwxrwxrwx+ hello-jni.c
drwxrwxrwt+ mathlib

./mathlib:
-rwxrwxrwx+ Android.mk
drwxrwxrwt+ add

./mathlib/add:
-rwxrwxrwx+ Android.mk
-rwxrwxrwx+ add.c
-rwxrwxrwx+ add.h
============================================================================================
hello-jni/jni/Application.mk contains single line
APP_MODULES := myadd hello-jni
============================================================================================
hello-jni/jni/Android.mk
--------------------------------
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_ARM_MODE := arm
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/mathlib/add
LOCAL_SHARED_LIBRARIES := myadd
LOCAL_LDLIBS :=-Lbuild/platforms/android-4/arch-arm/usr/lib -llog

include $(BUILD_SHARED_LIBRARY)
include $(LOCAL_PATH)/mathlib/Android.mk
============================================================================================
$ unzip -l HelloJni.apk
Archive: HelloJni.apk
Length Date Time Name
--------- ---------- ----- ----
1436 07-15-2010 18:55 AndroidManifest.xml
572 07-15-2010 17:51 resources.arsc
2400 07-15-2010 17:54 classes.dex
111696 05-06-2010 00:02 lib/armeabi/gdbserver
14174 07-15-2010 18:54 lib/armeabi/libhello-jni.so
13106 07-15-2010 18:54 lib/armeabi/libmyadd.so
485 07-15-2010 18:55 META-INF/MANIFEST.MF
538 07-15-2010 18:55 META-INF/CERT.SF
776 07-15-2010 18:55 META-INF/CERT.RSA
============================================================================================
some output from adb shell

# pwd
pwd
/data/data/com.example.hellojni/lib
# ls
ls
libmyadd.so
libhello-jni.so
============================================================================================

07-15 18:50:41.152: DEBUG/dalvikvm(1244): Trying to load lib /data/
data/com.example.hellojni/lib/libhello-jni.so 0x4375fca8
07-15 18:50:41.164: INFO/dalvikvm(1244): Unable to dlopen(/data/data/
com.example.hellojni/lib/libhello-jni.so): Cannot load library:
link_image[1638]: 553 could not load needed library 'libmyadd.so'
for 'libhello-jni.so' (load_library[984]: Library 'libmyadd.so' not
found)
07-15 18:50:41.164: WARN/dalvikvm(1244): Exception Ljava/lang/
UnsatisfiedLinkError; thrown during Lcom/example/hellojni/
HelloJni;.<clinit>
07-15 18:50:41.164: WARN/dalvikvm(1244): Class init failed in
newInstance call (Lcom/example/hellojni/HelloJni;)
07-15 18:50:41.174: DEBUG/AndroidRuntime(1244): Shutting down VM
07-15 18:50:41.174: WARN/dalvikvm(1244): threadid=3: thread exiting
with uncaught exception (group=0x4001aa28)
07-15 18:50:41.174: ERROR/AndroidRuntime(1244): Uncaught handler:
thread main exiting due to uncaught exception
07-15 18:50:41.193: ERROR/AndroidRuntime(1244):
java.lang.ExceptionInInitializerError
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.Class.newInstanceImpl(Native Method)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.Class.newInstance(Class.java:1472)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.Instrumentation.newActivity(Instrumentation.java:1097)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2316)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2417)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.ActivityThread.access$2100(ActivityThread.java:116)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.os.Handler.dispatchMessage(Handler.java:99)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.os.Looper.loop(Looper.java:123)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
android.app.ActivityThread.main(ActivityThread.java:4203)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.reflect.Method.invokeNative(Native Method)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.reflect.Method.invoke(Method.java:521)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
dalvik.system.NativeStart.main(Native Method)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): Caused by:
java.lang.UnsatisfiedLinkError: Library hello-jni not found
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.Runtime.loadLibrary(Runtime.java:489)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
java.lang.System.loadLibrary(System.java:557)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): at
com.example.hellojni.HelloJni.<clinit>(HelloJni.java:77)
07-15 18:50:41.193: ERROR/AndroidRuntime(1244): ... 15 more
============================================================================================

провод

unread,
Jul 15, 2010, 11:03:38 PM7/15/10
to andro...@googlegroups.com
Try to load the needed library explicitly (with System.load) before
you load hello-jni.

2010/7/15 Yadnesh <yad...@gmail.com>:

Yadnesh Phadke

unread,
Jul 16, 2010, 1:38:40 AM7/16/10
to andro...@googlegroups.com
Thanks.  This does work :)

But I think that it is a work around rather than the solution.  
Android SDK is trying to load libmyadd.so detecting that hello-jni.so needs it.  On which path does the SDK search for the library.  Is there a way to add the current path into paths that are searched for locating the library?

Regards,
Yadnesh

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--

----------------------------------------------------------------------
Christopher Morley - There is only one success - to be able to spend your life in your own way.

Sambhav

unread,
Jul 16, 2010, 1:45:13 AM7/16/10
to andro...@googlegroups.com
shared libraries are stored is "/system/lib"

Angus Lees

unread,
Jul 16, 2010, 2:03:27 AM7/16/10
to andro...@googlegroups.com
Since I read through this code only recently: The Android linker (at least post 2.0) supports LD_LIBRARY_PATH, but it only gets read at the start of the process - so you can't modify it from within your program to affect later dlopen()s, unfortunately.

It would be real nice if an app's native library directories (plural for armeabi-v7a) were in the standard dlopen() search path.  They aren't - and I can't think of any way to fix that without providing a different libdl implementation and using a replacement for System.loadLibrary that called into your libdl.  At the moment, Android searches the native libraries in the Java System.loadLibrary routines - once you hit the linker and start loading DT_NEEDED libraries, etc then the app-specific native library directories are forgotten.

 - Gus
Reply all
Reply to author
Forward
0 new messages