android中的不满意链接错误(eclipse)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6350329/
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
Unsatisfiedlinkerror in android (eclipse)
提问by d34th4ck3r
I am trying to run a simple jni code in Android, But all I am getting Unsatisfiedlinkerror .
我试图在 Android 中运行一个简单的 jni 代码,但我得到的都是 Unsatisfiedlinkerror 。
Here is my Java code:
这是我的Java代码:
package com.lipcap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a=new TextView(this);
String b;
MainActivity ob=new MainActivity();
b=ob.sniff();
a.setText(b);
setContentView(a);
}
public native String sniff();
static{
System.loadLibrary("native");
}
}
And here is My C++ code(in $PROJECT_PATH/jni/):
这是我的 C++ 代码(在 $PROJECT_PATH/jni/ 中):
#include<iostream>
#include<string.h>
#include<jni.h>
JNIEXPORT jstring JNICALL Java_com_lipcap_MainActivity_sniff
(JNIEnv *env, jobject obj){
return env->NewStringUTF("This is Native");
}
I have complied java code using javac, and made the header using javah.
我已经使用 javac 编译了 java 代码,并使用 javah 制作了标题。
Then I ran ndk-build. And then I ran code from eclipse.(installed apk in android).
然后我运行了 ndk-build。然后我从eclipse运行代码。(在android中安装了apk)。
I get this error:
我收到此错误:
E/AndroidRuntime( 769): FATAL EXCEPTION: main
E/AndroidRuntime( 769): java.lang.UnsatisfiedLinkError: sniff
E/AndroidRuntime( 769): at com.lipcap.MainActivity.sniff(Native Method)
E/AndroidRuntime( 769): at com.lipcap.MainActivity.onCreate(MainActivity.java:36)
E/AndroidRuntime( 769): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 769): at android.app.ActivityThread.access00(ActivityThread.java:125)
E/AndroidRuntime( 769): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 769): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 769): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 769): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 769): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 769): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 769): at dalvik.system.NativeStart.main(Native Method)
I have not set LD_LIBRARY_PATH.
我还没有设置 LD_LIBRARY_PATH。
However,without setting LD_LIBRARY_PATH sample code such as HelloJNI provided by NDK runs absolutely fine.
但是,如果不设置LD_LIBRARY_PATH 示例代码(例如NDK 提供的HelloJNI),则完全可以正常运行。
Please tell me where i am missing.
请告诉我我失踪的地方。
回答by yellowstonely
Richard-head you mentioned: "Changing code from C++ to C everything works fine"...
你提到的 Richard-head:“将代码从 C++ 更改为 C 一切正常”......
I was tortured by the exact same problem for several days and I did make sure everything typed by me (naming, Android.mk etc.) has no problem. Whenever in C, I'm fine. As long as I change to cpp, UnsatisfiedLinkError
.
我被完全相同的问题折磨了好几天,我确实确保我输入的所有内容(命名、Android.mk 等)都没有问题。每当在 C 中时,我都很好。只要我改变CPP, UnsatisfiedLinkError
。
I finally got the hint from this link: http://markmail.org/message/fhbnprmp2m7ju6lc
我终于从这个链接得到了提示:http: //markmail.org/message/fhbnprmp2m7ju6lc
It's all because of the C++ name mangling! The same function, if you don't have extern "C"
surrounding it in the .cpp file, the name is mangled so JNI can not find the function name so UnsatisfiedLinkError
pops up.
这都是因为 C++ 名称修改!同样的函数,如果你没有extern "C"
在 .cpp 文件中包含它,名称会被破坏,所以 JNI 找不到函数名称,所以UnsatisfiedLinkError
弹出。
Put on and remove the extern "C" { }
around your functions, run nm obj/local/armeabi/libnative.so
, you will clearly see the same function without and with name mangling.
穿上和移除extern "C" { }
你的函数周围,运行nm obj/local/armeabi/libnative.so
,你会清楚地看到没有和有名称修饰的相同函数。
I hope this helps others with the same problem too.
我希望这也能帮助其他有同样问题的人。
回答by Maximus
This isn't really been called quite right...
这不是真的被称为非常正确......
Try:
尝试:
package com.lipcap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView a;
public native String sniff();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a=new TextView(this);
String b = sniff();
a.setText(b);
setContentView(a);
}
static{
System.loadLibrary("native");
}
}
Lastly... is this in your Android.mk?
最后...这是在你的 Android.mk 中吗?
LOCAL_MODULE := native
回答by zoint
I will give an another advice.I got this same error before but I solved this problem via "Android Native Development Kit Cookbook".Please note these statements;
我会给出另一个建议。我之前遇到过同样的错误,但我通过“Android Native Development Kit Cookbook”解决了这个问题。请注意这些陈述;
The native function must follow a specific pattern for a package name, class name, and method name.The package and class name must agree with the package and class name of the Java class from which the native method is called, while the method name must be the same as the method name declared in that Java class. This helps the Dalvik VM to locate the native function at runtime.Failing to follow the rule will result in UnsatisfiedLinkError at runtime.
本机函数必须遵循特定模式的包名、类名和方法名。包和类名必须与调用本机方法的 Java 类的包和类名一致,而方法名必须与该 Java 类中声明的方法名称相同。这有助于 Dalvik VM 在运行时定位本机函数。不遵守规则将导致运行时出现 UnsatisfiedLinkError。
For example for above
例如上面
You need to change your function name like (don't use com.bla in the package names if you focus on NDK)
您需要更改您的函数名称(如果您专注于 NDK,请不要在包名称中使用 com.bla)
#include<iostream>
#include<string.h>
#include<jni.h>
JNIEXPORT jstring JNICALL Java_lipcap_example_MainActivity_sniff
(JNIEnv *env, jobject obj){
return env->NewStringUTF("This is Native");
}