JNI-java.lang.UnsatisfiedLinkError:找不到本机方法

JNI- java.lang.UnsatisfiedLinkError: Native method not found(JNI-java.lang.UnsatisfiedLinkError:找不到本机方法)
本文介绍了JNI-java.lang.UnsatisfiedLinkError:找不到本机方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenCV 开发一个 Android 项目.有些方法没有Java版本,所以我必须使用NDK在我的项目中使用它们.

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.

这是我第一次使用NDK,所以在搜索了一些示例后,我编写了代码,在我的设备上运行它,并收到以下错误消息:

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)

我多次重新生成 .so 文件,并检查 jni 函数的命名.我还在 jni 函数中添加了 'extern "C"' 包装器.但这些解决方案都没有奏效.

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.

注意:我一直使用 Intellij Idea 开发 JAVA 程序,由于 Idea 不支持 NDK,我必须使用 Eclipse 才能进入 NDK,所以……这也是我第一次使用 Eclipse.我本可以犯很多愚蠢的错误.哈哈

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

以下是我的 c++ 代码和 makefile.

Below are my c++ codes and makefile.

LocalMain.cpp

LocalMain.cpp

#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 和 LocalFeatures.cpp 没有显示,但我确定问题不在于它们.

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

Android.mk

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)

那些 .cpp 文件和 .mk 文件位于 Eclipse 项目中.libmytest_lib.so、libopencv_java.so、libnonfree.so 可以正确生成.以下 .java 文件位于 Intellij Idea 项目中.我已将这些 .so 文件复制到 Idea 项目中的 lib 中.

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.

JNIlib.java

JNIlib.java

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.
");
        }
    }
    public static native int readImg();
}

MainActivity.java 的一部分

Part of MainActivity.java

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*/
}

非常感谢!

推荐答案

按照 JonesV 的建议,我从代码中删除了静态.但在那之后,问题仍然存在.

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

这意味着,没有加载任何库.所以我检查了我的项目结构,发现我将 .so 文件放在 libs/文件夹中,而不是 libs/armeabi/文件夹中.

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.

解决方法在这里:Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null

这篇关于JNI-java.lang.UnsatisfiedLinkError:找不到本机方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)