2013年9月5日星期四

NDK using dynamic library ( shared library problems )

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

build a folder named JNI , after which add two documents, namely add.c and Android.mk, as shown below

add.c
#include <stdio.h>

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)


ndk-build after got libadd.so


(2) modify the hello-jni project

hello-jni is built Liezi , I tried to return the string before calling it a dynamic library of add (int, int) method , modified as follows

hello-jni.c
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <add.h>

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 file add a simple add.h
int add(int a, int b)


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

Finally, I certainly compile with NDK 7 , and in \ hello-jni \ libs \ armeabi has two libraries libadd.so and libhello-jni.so
library libadd.so, is automatically copied in the past, I did not manually copy the past

thought 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)


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


------ Solution ------------------------------------ --------
LOCAL_SHARED_LIBRARIES: =-ladd plus -l
------ Solution -------------- ------------------------------


LOCAL_SHARED_LIBRARIES: = add so yes

------ For reference only ---------------------------------- -----
to add System.loadLibrary ("add");
------ For reference only ------------------ ---------------------
ask later is how to solve it ? I also encountered the same problem, please enlighten me ~ Thank
------ For reference only -------------------------- -------------
Thomas082 answer right, to System.loadLibrary ("add");

没有评论:

发表评论