cant load library in JNI at run time

1,751 views
Skip to first unread message

joy joy

unread,
Mar 26, 2010, 2:45:04 AM3/26/10
to andro...@googlegroups.com

hi..

In my android application ,I used JNI for building .so files. now its works. but when i link it with the libchm.so file its cant load library. and my application failed. it cant find .so files in usr/lib directory. i dont know how to get path of the .so file..

please help me.



03-26 04:37:16.954: DEBUG/AndroidRuntime(537): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
03-26 04:37:17.008: DEBUG/AndroidRuntime(537): CheckJNI is ON
03-26 04:37:18.464: DEBUG/AndroidRuntime(537): --- registering native functions ---
03-26 04:37:26.216: DEBUG/AndroidRuntime(537): Shutting down VM
03-26 04:37:26.216: DEBUG/dalvikvm(537): DestroyJavaVM waiting for non-daemon threads to exit
03-26 04:37:26.216: DEBUG/dalvikvm(537): DestroyJavaVM shutting VM down
03-26 04:37:26.216: DEBUG/dalvikvm(537): HeapWorker thread shutting down
03-26 04:37:26.216: DEBUG/dalvikvm(537): HeapWorker thread has shut down
03-26 04:37:26.251: DEBUG/jdwp(537): JDWP shutting down net...
03-26 04:37:26.251: INFO/dalvikvm(537): Debugger has detached; object registry had 1 entries
03-26 04:37:26.280: DEBUG/dalvikvm(537): VM cleaning up
03-26 04:37:26.600: DEBUG/dalvikvm(537): LinearAlloc 0x0 used 625804 of 5242880 (11%)
03-26 04:37:28.893: DEBUG/AndroidRuntime(545): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
03-26 04:37:28.985: DEBUG/AndroidRuntime(545): CheckJNI is ON
03-26 04:37:30.366: DEBUG/AndroidRuntime(545): --- registering native functions ---
03-26 04:37:32.013: DEBUG/ddm-heap(545): Got feature list request
03-26 04:37:35.867: INFO/ActivityManager(57): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.FileLoaderActivity/.FileLoaderActivity }
03-26 04:37:36.097: DEBUG/AndroidRuntime(545): Shutting down VM
03-26 04:37:36.097: DEBUG/dalvikvm(545): DestroyJavaVM waiting for non-daemon threads to exit
03-26 04:37:36.206: ERROR/AndroidRuntime(545): ERROR: thread attach failed
03-26 04:37:36.251: DEBUG/dalvikvm(545): DestroyJavaVM shutting VM down
03-26 04:37:36.251: DEBUG/dalvikvm(545): HeapWorker thread shutting down
03-26 04:37:36.251: DEBUG/dalvikvm(545): HeapWorker thread has shut down
03-26 04:37:36.416: DEBUG/jdwp(545): JDWP shutting down net...
03-26 04:37:36.416: INFO/dalvikvm(545): Debugger has detached; object registry had 1 entries
03-26 04:37:36.453: DEBUG/dalvikvm(545): VM cleaning up
03-26 04:37:36.854: DEBUG/dalvikvm(545): LinearAlloc 0x0 used 636196 of 5242880 (12%)
03-26 04:37:37.455: INFO/ActivityManager(57): Start proc com.example.FileLoaderActivity for activity com.example.FileLoaderActivity/.FileLoaderActivity: pid=552 uid=10026 gids={}
03-26 04:37:38.477: DEBUG/ddm-heap(552): Got feature list request
03-26 04:37:40.085: DEBUG/dalvikvm(552): Trying to load lib /data/data/com.example.FileLoaderActivity/lib/libfileloader.so 0x43ab5a28
03-26 04:37:40.085: INFO/dalvikvm(552): Unable to dlopen(/data/data/com.example.FileLoaderActivity/lib/libfileloader.so): Cannot load library: link_image[1698]:    30 could not load needed library 'libchm.so.0' for 'libfileloader.so' (load_library[1039]: Library 'libchm.so.0' not found)
03-26 04:37:40.227: ERROR/JNI(552): WARNING: Could not load libNativeAdd.so
03-26 04:37:40.862: DEBUG/dalvikvm(552): +++ not scanning '/system/lib/libwebcore.so' for 'sayHello' (wrong CL)
03-26 04:37:40.882: DEBUG/dalvikvm(552): +++ not scanning '/system/lib/libmedia_jni.so' for 'sayHello' (wrong CL)
03-26 04:37:40.900: DEBUG/dalvikvm(552): +++ not scanning '/system/lib/libexif.so' for 'sayHello' (wrong CL)
03-26 04:37:40.900: DEBUG/dalvikvm(552): +++ not scanning '/system/lib/libsrec_jni.so' for 'sayHello' (wrong CL)
03-26 04:37:40.900: WARN/dalvikvm(552): No implementation found for native Lcom/example/FileLoaderActivity/FileLoaderActivity;.sayHello ()Ljava/lang/String;
03-26 04:37:40.900: DEBUG/AndroidRuntime(552): Shutting down VM
03-26 04:37:40.900: WARN/dalvikvm(552): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-26 04:37:40.900: ERROR/AndroidRuntime(552): Uncaught handler: thread main exiting due to uncaught exception
03-26 04:37:41.155: ERROR/AndroidRuntime(552): java.lang.UnsatisfiedLinkError: sayHello
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at com.example.FileLoaderActivity.FileLoaderActivity.sayHello(Native Method)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at com.example.FileLoaderActivity.FileLoaderActivity.onCreate(FileLoaderActivity.java:31)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.os.Looper.loop(Looper.java:123)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at android.app.ActivityThread.main(ActivityThread.java:4310)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at java.lang.reflect.Method.invokeNative(Native Method)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at java.lang.reflect.Method.invoke(Method.java:521)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-26 04:37:41.155: ERROR/AndroidRuntime(552):     at dalvik.system.NativeStart.main(Native Method)
03-26 04:37:41.314: INFO/Process(57): Sending signal. PID: 552 SIG: 3
03-26 04:37:41.372: INFO/dalvikvm(552): threadid=7: reacting to signal 3
03-26 04:37:41.800: INFO/dalvikvm(552): Wrote stack trace to '/data/anr/traces.txt'
03-26 04:37:46.083: WARN/ActivityManager(57): Launch timeout has expired, giving up wake lock!
03-26 04:37:49.245: WARN/ActivityManager(57): Activity idle timeout for HistoryRecord{43c1f050 com.example.FileLoaderActivity/.FileLoaderActivity}
03-26 04:37:54.703: DEBUG/dalvikvm(159): GC freed 197 objects / 9016 bytes in 371ms









thnx.

mic _

unread,
Mar 26, 2010, 4:00:27 AM3/26/10
to andro...@googlegroups.com
If your library relies on some other library (e.g. libchm) that isn't
part of Android, then you need to build that library yourself.

/Michael

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

Addy

unread,
Mar 26, 2010, 8:04:59 AM3/26/10
to android-ndk
Thnx for reply..

In my application, I build libchm.so with the JNI. and after generated
libchm.so file, I put the file in the data/data/<project_folder>/lib
still it cant load from the lib.

Here is my steps for building .so file

1. Create a java file in which I call native method of my JNI.
2. After it, complie with javah and make .h header file.
3. And then in my project I make couple of .mk file 1. Android.mk and
2. Application.mk
4. Then i create .c file for my native method implementation (which is
use libchm.so file for methods).
5. Then compile it with make APP=<project_name> from the root of the
my ANDROID_NDK directory.
And build libchm.so file and this file I put in project/lib. But, at
the running time the libchm.so library cant load.

And I also put needed .so files in NDK/platform/arm-embi/usr/lib but
still the problem arise.

This is my Android.mk file,

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := test_demo

LOCAL_SRC_FILES := test_demo.c chm_lib.h

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lchm

include $(BUILD_SHARED_LIBRARY)

I dont know, what happened?

Thnx..

mic _

unread,
Mar 26, 2010, 8:18:00 AM3/26/10
to andro...@googlegroups.com
Did you give it the right filename then? libfileloader seems to rely
on libchm.so.0.

/Michael

Hans-Werner Hilse

unread,
Mar 26, 2010, 5:59:34 AM3/26/10
to andro...@googlegroups.com
Hi,

2010/3/26 joy joy <joyy....@gmail.com>

03-26 04:37:40.085: INFO/dalvikvm(552): Unable to dlopen(/data/data/com.example.FileLoaderActivity/lib/libfileloader.so): Cannot load library: link_image[1698]:    30 could not load needed library 'libchm.so.0' for 'libfileloader.so' (load_library[1039]: Library 'libchm.so.0' not found)

It's looking in /data/data/<app id>/lib/ (as you can see from your own cited debug output above). One problem that is clearly visible is that your libchm doesn't carry a .so suffix but rather a .so.0 suffix. See recent threads on this ML for a discussion of such version appendixes. Basically, they aren't recognized by the APK packetizer (or whatever that is called) and so they are not installed in the mentioned directory upon APK installation. So you have to make sure all your libs have a .so suffix.

Hmmmm, and: I remember there have been problems w/ dlopen(), are those fixed now?

-hwh

Addy

unread,
Mar 29, 2010, 2:31:23 AM3/29/10
to android-ndk
Hi HWH.
Thnx for reply..

As per your suggestion, I go through the .so.0 file. but machine find
the library at the run time from the system/lib directory.
And I cant put the my .so file in the system/lib directory (Error :
Read only file system).
so how can I put .so file in system/lib directory.

or if any other solution for dynamic load library at the run time.

Thnx in advance.

Joyy

mic _

unread,
Mar 29, 2010, 2:46:29 AM3/29/10
to andro...@googlegroups.com
You shouldn't write anything to /system/lib (and as you noticed, you
can't). I think the point that Hans-Werner was making was that the
files that you put in /libs/armeabi in the eclipse workspace for your
Android app should have the extension .so (not, .so.0, .so.1 or
anything like that). Those libraries that have a proper extension will
be installed automatically in the app's own lib directory on the
device. If you've got inter-dependencies among your libraries (i.e.
library A is linked against library B), then you need to make sure
that they are linked against versions of eachother that only have a
.so extension.

/Michael

Addy

unread,
Mar 29, 2010, 3:07:50 AM3/29/10
to android-ndk

Eugene Mymrin

unread,
Mar 29, 2010, 3:44:58 AM3/29/10
to andro...@googlegroups.com
If what you do is calling dlopen from native library then you don't
need to put the library into system/lib.
Put the library in your app libs folder, call
System.loadLibrary("xxx") from Java, then call dlopen("libxxx.so")
from JNI.

Addy

unread,
Mar 29, 2010, 3:46:12 AM3/29/10
to android-ndk
can i send you whole code of application?

Addy

unread,
Mar 29, 2010, 4:39:28 AM3/29/10
to android-ndk

Addy

unread,
Mar 29, 2010, 4:53:45 AM3/29/10
to android-ndk

Addy

unread,
Mar 29, 2010, 5:04:41 AM3/29/10
to android-ndk
Actually in My application I want to read chm books.
for this I used chm arm compiled .so files.
In my java file, loadLibrary("fileloader") call native JNI and in my
JNI C file contains some functions from the chm.so file.
At the compilation is completed. I put the .so generated file in data/
<project>/lib and then Run the program.
But at the runtime machine cant load the .so file.
It cant find the .so file.
In Android.mk file I put
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lchm,
and in usr/lib i put the all chm .so file generated by the arm gcc
compiler.
still the file doesn't load.

Addy

unread,
Mar 29, 2010, 5:04:59 AM3/29/10
to android-ndk

Addy

unread,
Mar 29, 2010, 5:41:55 AM3/29/10
to android-ndk

Eugene Mymrin

unread,
Mar 29, 2010, 5:42:23 AM3/29/10
to andro...@googlegroups.com
First, you must compile chm.so (or chm.a) using NDK.
Second, you should link your native library against chm.so (or chm.a);
use LOCAL_SHARED_LIBRARIES := libchm (or LOCAL_STATIC_LIBRARIES :=
libchm) for that.
Put resulting .so file(s) into /libs/armeabi subfolder of your solution folder.
In Java, if you link to chm library dynamically, call System.loadLibrary("chm").
Call System.loadLibrary("your_lib").

The best choice is to link to chm library statically.
If you have to link dynamically, your native library won't load in
Android 1.5 due to a bug in OS (but there's at least one workaround I
know about).

Reply all
Reply to author
Forward
0 new messages