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

后端 未结 4 957
自闭症患者
自闭症患者 2020-12-10 05:43

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

相关标签:
4条回答
  • 2020-12-10 06:22

    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);
    
    0 讨论(0)
  • 2020-12-10 06:23

    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

    0 讨论(0)
  • 2020-12-10 06:29

    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

    0 讨论(0)
  • 2020-12-10 06:41

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

    extern "C" JNIEXPORT jstring JNICALL
    Java_package_MyClass_test(JNIEnv* env, jobject)
    
    0 讨论(0)
提交回复
热议问题