3

I wrote a sample shared library in VS 2015 (NDK Cross Platform project)

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 now i have a .so file and i want to create a project in Android Studio which link against that .so file and call first(x,y) with JNI.

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); 
}

So I put libCommon.so and Common.h in jni folder of the project edited the 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)

(I added LOCAL_ALLOW_UNDEFINED_SYMBOLS := true since I got an error undefined reference to 'first')

Application.mk:

APP_ABI := armeabi-v7a

So now I have two .so files in src\main\libs\armeabi-v7a. The application is crashed when I load libhello_jni.so and it isn't crashed for loading libCommon.so

static
{
   System.loadLibrary("Common");
   System.loadLibrary("hello_jni");
}

Error log:

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)

I don't know what to do ... Why first function not found ? Any ideas?

Thanks!

1337
  • 317
  • 1
  • 9

0 Answers0