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

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:

System.loadLibrary("opencv_java");

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 ex:

extern "C"
jstring
Java_lara_myapplication_MainActivity_stringFromJNI(
        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);