title: NDK - call a native function from .so file
body: <p>I wrote a sample shared library in VS 2015 (NDK Cross Platform project)</p>
  
  <p>Common.h:</p>
  
  <pre><code>#ifdef __cplusplus
     extern "C" {
  #endif
  
  int first(int  x, int  y);
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>Common.cpp:</p>
  
  <pre><code>#include "Common.h"
  
  int first(int  x, int  y)
  {
       return x + y;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>hello_jni.c:</p>
  
  <pre><code>#include "Common.h"
  #include &lt;jni.h&gt;
  
  jint
  Java_com_test_MainActivity_add( JNIEnv* env,
                                      jobject thiz,
                                      jint a,
                                      jint b)
  {
      return (jint)first((int)a,(int)b); 
  }
  </code></pre>
  
  <p>So I put libCommon.so and Common.h in jni folder of the project
  edited the android.mk:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>(I added LOCAL_ALLOW_UNDEFINED_SYMBOLS := true since I got an error undefined reference to 'first')</p>
  
  <p>Application.mk:</p>
  
  <pre><code>APP_ABI := armeabi-v7a
  </code></pre>
  
  <p>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 </p>
  
  <pre><code>static
  {
     System.loadLibrary("Common");
     System.loadLibrary("hello_jni");
  }
  </code></pre>
  
  <p>Error log:</p>
  
  <pre><code>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.&lt;clinit&gt;(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)
  </code></pre>
  
  <p>I don't know what to do ... Why first function not found ? 
  Any ideas? </p>
  
  <p>Thanks!</p>