Friday, February 28, 2014

NDK using dynamic library (shared library problem)


posts by Thomas082 edited 2012-03-20 23:47:15
(1) to generate dynamic library

Build a file folder named JNI, after which add two documents, namely add.c and Android.mk,
shown below
add.c
 # include  

int add (int a, int b)
{
return a + b;
}


Android.mk
 LOCAL_PATH: = $ (call my-dir) 

include $ (CLEAR_VARS)

LOCAL_MODULE: = add
LOCAL_SRC_FILES: = add.c

include $ (BUILD_SHARED_LIBRARY)


After ndk-build, got libadd.so


(2) modify the hello-jni project

is as follows
hello-jni comes Lie, I tried to return the string before calling about the dynamic library add (int, int) method, the modified
hello-jni.c
 # include  
# Include
# Include
# Include

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI (JNIEnv * env,
jobject thiz)
{
add (1, 2);
return (* env) -> NewStringUTF (env, "Hello from JNI!");
}


Android.mk
 LOCAL_PATH: = $ (call my-dir) 

include $ (CLEAR_VARS)

LOCAL_MODULE: = add
LOCAL_SRC_FILES: = libadd.so
LOCAL_EXPORT_C_INCLUDES: = $ (LOCAL_PATH) / include

include $ (PREBUILT_SHARED_LIBRARY)

include $ (CLEAR_VARS)

LOCAL_MODULE: = hello-jni
LOCAL_SRC_FILES: = hello-jni.c
LOCAL_SHARED_LIBRARIES: = add

include $ (BUILD_SHARED_LIBRARY)


Yes, I'm still include files add a simple add.h
 int add (int a, int b) 


Of course, I also added a JNI directory libadd.so

Finally, I would certainly use the NDK 7 compiled through, and \ hello-jni \ libs \ armeabi have two libraries libadd.so and libhello-jni.so
Library libadd.so, is automatically in the past, not the past
I manually copy copy
Originally feel that this should be no problem under the bar, but throw an error:
03-20 23:17:30.434: D / dalvikvm (192): Trying to load lib / data / data / com.example.hellojni / lib / libhello-jni.so 0x43757ca8
03-20 23:17:30.474: I / dalvikvm (192): Unable to dlopen (/ data / data / com.example.hellojni / lib / libhello-jni.so): Cannot load library: link_image [1638]: 30 could not load needed library 'libadd.so' for 'libhello-jni.so' (load_library [984]: Library 'libadd.so' not found) < br />

I do not understand why? We have encountered this problem? How to solve it?

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
LOCAL_SHARED_LIBRARIES: =-ladd plus-l
Reply:


LOCAL_SHARED_LIBRARIES: = add this right

Reply:
To add System.loadLibrary ("add");
Reply:
Later I ask is how to solve it? I also encountered the same problem, please enlighten me ~ Thank you
Reply:
Thomas082 answer right, to System.loadLibrary ("add");
Reply:
The landlord Hello, may I ask "
you mentioned in the articleYes, I'm still include files add a simple add.h "
This include file is where what is meant? ? ? ?

No comments:

Post a Comment