Android JNI- java.lang.UnsatisfiedLinkError:未找到本机方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24566127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JNI- java.lang.UnsatisfiedLinkError: Native method not found
提问by pwwpche
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.
我正在使用 OpenCV 开发一个 Android 项目。有些方法没有Java版本,所以我必须使用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:
这是我第一次使用 NDK,因此在搜索了一些示例后,我编写了代码,在我的设备上运行它,并收到以下错误消息:
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.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.
我多次重新生成 .so 文件,并检查 jni 函数的命名。我还在 jni 函数中添加了 'extern "C"' 包装器。但是这些解决方案都没有奏效。
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
注意:我一直使用Intellij Idea来开发JAVA程序,而且由于Idea不支持NDK,我必须使用Eclipse才能进入NDK,所以……这也是我第一次使用Eclipse。我本可以犯很多愚蠢的错误。哈哈
Below are my c++ codes and makefile.
下面是我的 C++ 代码和 makefile。
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 and LocalFeatures.cpp are not shown, but I'm sure the problem is not with them.
LocalFeatures.h 和 LocalFeatures.cpp 未显示,但我确定问题不在于它们。
Android.mk
安卓.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)
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.
这些 .cpp 文件和 .mk 文件位于 Eclipse 项目中。可以正确生成libmytest_lib.so、libopencv_java.so、libnonfree.so。以下 .java 文件位于 Intellij Idea 项目中。我已将这些 .so 文件复制到 Idea 项目中的 \lib 中。
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.\n");
}
}
public static native int readImg();
}
Part of MainActivity.java
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*/
}
Thanks a lot!
非常感谢!
采纳答案by pwwpche
After following JonesV's advice, I removed static from my code. But after that, the problem still exists.
在遵循 JonesV 的建议之后,我从我的代码中删除了静态。但在那之后,问题仍然存在。
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.
这意味着,没有加载 LIBS。所以我检查了我的项目结构,发现我把我的 .so 文件放在 libs/ 文件夹中,而不是 libs/armeabi/ 文件夹中。
And the solution is found here: Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null
解决方法在这里找到: Android NDK java.lang.UnsatisfiedLinkError: findLibrary returned null
回答by Felix Cruz
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
然后错误消失了
回答by Sekhar Madhiyazhagan
Remove static
from:
static
从以下位置移除:
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
):
如果你真的希望你的readImg()
方法是静态的,你应该如下声明 JNI 方法(用jclass
而不是jobject
):
JNIEXPORT jint JNICALL Java_com_example_MyTest_JNIlib_readImg(JNIEnv * env, jclass obj);
回答by ENSATE
For me I was using the wrong class (MyClass) in
对我来说,我使用了错误的类(MyClass)
extern "C" JNIEXPORT jstring JNICALL
Java_package_MyClass_test(JNIEnv* env, jobject)