title: JNI- java.lang.UnsatisfiedLinkError: Native method not found
body: <p>I'm developing an Android project using OpenCV. Some of the methods do not have Java version, so I have to use NDK to use them in my project.</p>
  
  <p>This is my first time using NDK, so after searching for some examples, I wrote my code, run it on my device, and received error message below:</p>
  
  <pre><code>07-04 10:26:19.555  21270-21270/com.example.MyTest E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.UnsatisfiedLinkError: Native method not found: com.example.MyTest.JNIlib.readImg:()I
              at com.example.MyTest.JNIlib.readImg(Native Method)
              at com.example.MyTest.MyActivity$2.onClick(MyActivity.java:60)
              at android.view.View.performClick(View.java:4211)
              at android.view.View$PerformClick.run(View.java:17267)
              at android.os.Handler.handleCallback(Handler.java:615)
              at android.os.Handler.dispatchMessage(Handler.java:92)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4898)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I regenerated .so files for several times, and check the naming for jni functions. I also added 'extern "C"' wrappers to jni functions. But none of those solutions worked. </p>
  
  <p>Notice: I always use Intellij Idea to develop JAVA programs, and since Idea doesn't support NDK, and I have to use Eclipse to get to NDK, so...this is also the first time I use Eclipse. I could have made a lot of silly mistakes. lol</p>
  
  <p>Below are my c++ codes and makefile.</p>
  
  <p>LocalMain.cpp</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;opencv2/nonfree/nonfree.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/nonfree/features2d.hpp&gt;
  
  #include &lt;opencv2/opencv.hpp&gt;  
  #include &lt;jni.h&gt;
  #include &lt;android/log.h&gt;
  #include "LocalFeature.h"
  using namespace cv;  
  using namespace std;  
  
  
  
  int readImg()
  {  
      /* do something*/
  
      return 0;  
  }  
  
  
  // JNI interface functions, be careful about the naming.
  extern "C"
  {
      JNIEXPORT jint JNICALL Java_com_example_MyTest_JNIlib_readImg(JNIEnv * env, jobject obj);
  };
  
  JNIEXPORT jint JNICALL Java_com_example_MyTest_JNIlib_readImg(JNIEnv * env, jobject obj)
  {
      return (jint)readImg();
  }
  </code></pre>
  
  <p>LocalFeatures.h and LocalFeatures.cpp are not shown, but I'm sure the problem is not with them.</p>
  
  <p>Android.mk</p>
  
  <pre><code>LOCAL_PATH:= $(call my-dir)
  
  include $(CLEAR_VARS)
  LOCAL_MODULE    := nonfree_prebuilt
  LOCAL_SRC_FILES := libnonfree.so
  include $(PREBUILT_SHARED_LIBRARY)
  
  include $(CLEAR_VARS)
  LOCAL_MODULE    := opencv_java_prebuilt
  LOCAL_SRC_FILES := libopencv_java.so
  include $(PREBUILT_SHARED_LIBRARY)
  
  include $(CLEAR_VARS)
  # Modify LOCAL_C_INCLUDES with your path to OpenCV for Android.
  LOCAL_C_INCLUDES:= E:/Java/libs/OpenCV-2.4.9-android-sdk/sdk/native/jni/include
  LOCAL_MODULE    := mytest_lib
  LOCAL_CFLAGS    := -Werror -O3 -ffast-math
  LOCAL_LDLIBS    += -llog -ldl 
  LOCAL_SHARED_LIBRARIES := nonfree_prebuilt opencv_java_prebuilt
  LOCAL_SRC_FILES := LocalMain.cpp \
  LocalFeature.h \
  LocalFeature.cpp
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>Those .cpp files and .mk files are in an Eclipse project. libmytest_lib.so, libopencv_java.so, libnonfree.so can be correctly generated. 
  The following .java files are in an Intellij Idea project. I have copied these .so files to \lib in the Idea project.</p>
  
  <p>JNIlib.java </p>
  
  <pre><code>public class JNIlib {
      static
      {
          try
          {
              // Load necessary libraries.
              System.loadLibrary("opencv_java");
              System.loadLibrary("nonfree");
              System.loadLibrary("mytest_lib");
          }
          catch( UnsatisfiedLinkError e )
          {
              System.err.println("Native code library error.\n");
          }
      }
      public static native int readImg();
  }
  </code></pre>
  
  <p>Part of MainActivity.java</p>
  
  <pre><code>public class MyActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2{
  
      public View.OnClickListener onClickListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              JNIlib.readImg();
          }
      };
  
      /*Other irrevelant code*/
  }
  </code></pre>
  
  <p>Thanks a lot!</p>