Android app shared library dependency

2,362 views
Skip to first unread message

Jin Chiu

unread,
Feb 13, 2010, 11:16:37 AM2/13/10
to android-ndk
Is it possible for an Android app to load custom shared library that
depends on other custom shared libraries associated with this app? To
give a concrete example, I would like my app to load shared library
"A.so" that is dynamically linked against "B.so" and "C.so". If
possible, how would I specify the dependencies in Android build
system? Where would I place B.so and C.so in the app directory tree?

Divkis

unread,
Feb 17, 2010, 12:18:26 AM2/17/10
to android-ndk
Hi,

I have faced similar issue but in a different context. I was
assuming that the Android build system would automatically add the
dependency and load the dependent shared libraries. But apparently
that is not the case. You will need to explicitly load the dependent
libraries yourself, which are not system libraries.

hope that helps,
DivKis

David Turner

unread,
Feb 18, 2010, 5:33:02 PM2/18/10
to andro...@googlegroups.com
The Android dynamic linker had a bug that prevented this from working, but was fixed in 1.6, I believe.
If you use the NDK, use "LOCAL_SHARED_LIBRARIES := libB libC" when defining the libA module.
This assumes that libB and libC are also NDK modules that were generated with the NDK.

In case libB.so and libC.so are not generated with the NDK, you should do the following:

  • in the libA module definition, use LOCAL_LDLIBS += /full/path/to/libB.so /full/path/to/libC.so
    this ensures that correct symbol exports are generated in libA.so

  • manually copy libB.so and libC.so to $APP_PROJECT/libs/armeabi before rebuilding your .apk,
    this ensures that it will be copied to /data/data/<pkgname>/lib at installation time by the package manager.

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


Jin Chiu

unread,
Mar 4, 2010, 9:45:52 AM3/4/10
to android-ndk
I tried adding LOCAL_SHARED_LIBRARIES+=libB, where libB is a NDK
shared library module. I then wrote a test app to load libA and libB
in an attempt to use native interfaces defined in libA. However at
runtime, I'm getting a link error telling me that libB.so cannot be
found, even though both libA.so and libB.so were copied to /data/data/
<app>/lib. Is the program loader expecting libB to be located
elsewhere? Is there a way in Android.mk to specify to program loader
where to search for libB at runtime (i.e. -Wl,-rpath)?

On Feb 18, 2:33 pm, David Turner <di...@android.com> wrote:
> The Android dynamic linker had a bug that prevented this from working, but
> was fixed in 1.6, I believe.
> If you use the NDK, use "LOCAL_SHARED_LIBRARIES := libB libC" when defining
> the libA module.
> This assumes that libB and libC are also NDK modules that were generated
> with the NDK.
>
> In case libB.so and libC.so are not generated with the NDK, you should do
> the following:
>

>    - in the libA module definition, use LOCAL_LDLIBS +=


>    /full/path/to/libB.so /full/path/to/libC.so
>    this ensures that correct symbol exports are generated in libA.so
>

>    - manually copy libB.so and libC.so to $APP_PROJECT/libs/armeabi before


>    rebuilding your .apk,
>    this ensures that it will be copied to /data/data/<pkgname>/lib at
>    installation time by the package manager.
>

> On Sat, Feb 13, 2010 at 8:16 AM, Jin Chiu <live2drea...@gmail.com> wrote:
> > Is it possible for an Android app to load custom shared library that
> > depends on other custom shared libraries associated with this app? To
> > give a concrete example,  I would like my app to load shared library
> > "A.so" that is dynamically linked against "B.so" and "C.so". If
> > possible, how would I specify the dependencies in Android build
> > system? Where would I place B.so and C.so in the app directory tree?
>
> > --
> > 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<android-ndk%2Bunsu...@googlegroups.com>

David Turner

unread,
Mar 4, 2010, 7:32:37 PM3/4/10
to andro...@googlegroups.com
It is a known bug of the Android 1.5 dynamic linker that was solved in 1.6.
If you need to target 1.5, link B statically to A.

To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Jin Chiu

unread,
Mar 5, 2010, 8:50:25 PM3/5/10
to android-ndk
I'm actually seeing this behavior with NDK 1.6r1 with an emulator
running 1.6 platform...

As a simple test setup, I first create a standard shared lib module
called "mylib.so". I then create a separate module called "mylib-
user.so" and set LOCAL_SHARED_LIBRARIES := mylib in its makefile.
Finally, have my java app performs `System.loadLibrary("mylib-user")`.
When I try run the app from Eclipse, I receive the following
exception:

D/dalvikvm( 216): Trying to load lib /data/data/com.example.hellojni/
lib/libmylib-user.so 0x43757368
I/dalvikvm( 216): Unable to dlopen(/data/data/com.example.hellojni/
lib/libmylib-user.so): Cannot load library: link_image[1638]: 29
could not load needed library 'libmylib.so' for 'libmylib-
user.so' (load_library[984]: Library 'libmylib.so' not found)
W/dalvikvm( 216): Exception Ljava/lang/UnsatisfiedLinkError; thrown
during Lcom/example/hellojni/HelloJni;.<clinit>

Even though libmylib.so is in the same directory as libmylib-user.so:
# pwd
/data/data/com.example.hellojni/lib
# ls
libmylib.so
libmylib-user.so

libmylib.so itself implements a native interface that works fine when
used directly, so there's nothing wrong with libmylib.so


On Mar 4, 4:32 pm, David Turner <di...@android.com> wrote:
> It is a known bug of the Android 1.5 dynamic linker that was solved in 1.6.
> If you need to target 1.5, link B statically to A.
>

> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages