pwwpche pwwpche - 1 year ago 132
Android Question

JNI- java.lang.UnsatisfiedLinkError: Native method not found

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.

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:

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(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(
at dalvik.system.NativeStart.main(Native Method)

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.

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

Below are my c++ codes and makefile.


#include <stdio.h>
#include <iostream>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/nonfree/features2d.hpp>

#include <opencv2/opencv.hpp>
#include <jni.h>
#include <android/log.h>
#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();

LocalFeatures.h and LocalFeatures.cpp are not shown, but I'm sure the problem is not with them.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := nonfree_prebuilt

include $(CLEAR_VARS)
LOCAL_MODULE := opencv_java_prebuilt

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 \


Those .cpp files and .mk files are in an Eclipse project.,, 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.

public class JNIlib {
// Load necessary libraries.
catch( UnsatisfiedLinkError e )
System.err.println("Native code library error.\n");
public static native int readImg();

Part of

public class MyActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2{

public View.OnClickListener onClickListener = new View.OnClickListener() {
public void onClick(View v) {

/*Other irrevelant code*/

Thanks a lot!

Answer Source

After following JonesV's advice, I removed static from my code. But after that, the problem still exists.

Finally I debugged my my program, and when running to this line:


The program threw an error, instead of running normally as I thought. REALLY STUPID MISTAKE :<

Now that I found an error I'd never noticed before.

java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null

Which means, NO LIBS ARE LOADED. So I checked my project structure, and found that I put my .so files in libs/ folder, instead of libs/armeabi/ folder.

And the solution is found here: Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null by user1647750

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download