Home » Android » android – JNI- java.lang.UnsatisfiedLinkError: Native method not found

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

Posted by: admin June 15, 2020 Leave a comment


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(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)

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
LOCAL_SRC_FILES := libnonfree.so

include $(CLEAR_VARS)
LOCAL_MODULE    := opencv_java_prebuilt
LOCAL_SRC_FILES := libopencv_java.so

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. 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.


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

Part of MainActivity.java

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!

How to&Answers:

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


I added

extern "C"

before functions declare

extern "C"
        JNIEnv *env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());

then the error was gone


Remove static from:

public static native int readImg(); 

i.e. write it like this:

public native int readImg();

If you really want your readImg() method to be static, you should declare the JNI method as follows (with jclass instead of jobject):

 JNIEXPORT jint JNICALL Java_com_example_MyTest_JNIlib_readImg(JNIEnv * env, jclass obj);


For me I was using the wrong class (MyClass) in

extern "C" JNIEXPORT jstring JNICALL
Java_package_MyClass_test(JNIEnv* env, jobject)