的UnsatisfiedLinkError在机器人(蚀) [英] Unsatisfiedlinkerror in android (eclipse)

查看:98
本文介绍了的UnsatisfiedLinkError在机器人(蚀)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个简单的JNI code在Android的,但是,所有我得到的UnsatisfiedLinkError。

I am trying to run a simple jni code in Android, But all I am getting Unsatisfiedlinkerror .

下面是我的Java code:

Here is my Java code:

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");
    }


} 

下面是我的C ++ code(在$ PROJECT_PATH / JNI /):

And here is My C++ code(in $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");
}

我一直遵守的java code。使用javac和javah的使用做了头。

I have complied java code using javac, and made the header using javah.

然后我跑NDK建造。 然后我跑了code从蚀。(Android中安装APK)。

Then I ran ndk-build. And then I ran code from eclipse.(installed apk in android).

我得到这个错误:

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.access$2300(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)

我没有设置LD_LIBRARY_PATH。

I have not set LD_LIBRARY_PATH.

然而,如果没有设置LD_LIBRARY_PATH样品code如NDK提供HelloJNI运行完全正常。

However,without setting LD_LIBRARY_PATH sample code such as HelloJNI provided by NDK runs absolutely fine.

请告诉我,我的思念。

推荐答案

理查德头你所提到的:更改code,从C ++到C一切正常...

Richard-head you mentioned: "Changing code from C++ to C everything works fine"...

我被折磨的完全相同的问题好几天了,我做了确保一切由我输入(命名,Android.mk等)没有问题。每当C,我很好。只要我改变CPP,的UnsatisfiedLinkError

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.

我终于得到了这个链接的提示: <一href="http://markmail.org/message/fhbnprmp2m7ju6lc">http://markmail.org/message/fhbnprmp2m7ju6lc

I finally got the hint from this link: http://markmail.org/message/fhbnprmp2m7ju6lc

这是C ++的名字改编的一切都是因为!同样的功能,如果你没有的externC周围的.cpp文件,名字是错位所以JNI找不到函数的名称,以便的UnsatisfiedLinkError 弹出。

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.

放在与删除的externC{} 在你的功能,运行纳米OBJ /本地/ armeabi / libnative.so ,你会清楚地看到同样的功能,而不与名字粉碎。

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.

我希望这可以帮助其他有同样的问题太多。

I hope this helps others with the same problem too.

这篇关于的UnsatisfiedLinkError在机器人(蚀)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆