NDK - 从.so文件中调用本机函数

时间:2016-02-03 17:58:23

标签: android c++ android-ndk java-native-interface

我在VS 2015(NDK跨平台项目)中编写了一个示例共享库

COMMON.H:

#ifdef __cplusplus
   extern "C" {
#endif

int first(int  x, int  y);

#ifdef __cplusplus
}
#endif

Common.cpp:

#include "Common.h"

int first(int  x, int  y)
{
     return x + y;
}

所以现在我有一个.so文件,我想在Android Studio中创建一个项目,该项目链接到.so文件并首先用JNI调用(x,y)。

hello_jni.c:

#include "Common.h"
#include <jni.h>

jint
Java_com_test_MainActivity_add( JNIEnv* env,
                                    jobject thiz,
                                    jint a,
                                    jint b)
{
    return (jint)first((int)a,(int)b); 
}

所以我将libCommon.so和Common.h放在项目的jni文件夹中 编辑了android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libCommon.so
LOCAL_MODULE := add_prebuilt
LOCAL_EXPORT_C_INCLUDES := Common.h
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES  := hello_jni.c
LOCAL_MODULE     :=  hello_jni
LOCAL_SHARED_LIBRARIES := add_prebuilt
include $(BUILD_SHARED_LIBRARY)

(我添加了LOCAL_ALLOW_UNDEFINED_SYMBOLS:= true,因为我得到了错误的未定义引用&#39;首先&#39;)

Application.mk:

APP_ABI := armeabi-v7a

所以现在我在src \ main \ libs \ armeabi-v7a中有两个.so文件。 加载libhello_jni.so时应用程序崩溃了  加载libCommon.so

并没有崩溃
static
{
   System.loadLibrary("Common");
   System.loadLibrary("hello_jni");
}

错误日志:

02-03 12:52:52.509 9685-9685/com.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                        Process: com.test, PID: 9685
                                                        java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "first" referenced by "libhello_jni.so"...
                                                            at java.lang.Runtime.loadLibrary(Runtime.java:364)
                                                            at java.lang.System.loadLibrary(System.java:526)
                                                            at com.test.MainActivity.<clinit>(MainActivity.java:23)
                                                            at java.lang.Class.newInstanceImpl(Native Method)
                                                            at java.lang.Class.newInstance(Class.java:1208)
                                                            at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                            at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                            at android.os.Looper.loop(Looper.java:136)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                            at dalvik.system.NativeStart.main(Native Method)

我不知道该怎么做...为什么找不到第一个功能? 有任何想法吗?

谢谢!

0 个答案:

没有答案