现在的位置: 首页 > 综合 > 正文

No JNI_OnLoad found in /data/data/

2018年05月16日 ⁄ 综合 ⁄ 共 3924字 ⁄ 字号 评论关闭

0. 些在前面: 最近,又用到ndk去进行jni的开发了,居然连最简单的hello-jni都没有编译过。

1. 报错如下:

D/dalvikvm(16064): Trying to load lib /data/data/factorytest.android.com/lib/libhello-jni.so 0x42117dc0
D/dalvikvm(16064): Added shared lib /data/data/factorytest.android.com/lib/libhello-jni.so 0x42117dc0
D/dalvikvm(16064): No JNI_OnLoad found in /data/data/factorytest.android.com/lib/libhello-jni.so 0x42117dc0, skipping init
W/dalvikvm(16064): No implementation found for native Lcom/example/hellojni/hellotest;.stringFromJNI:()Ljava/lang/String;
D/AndroidRuntime(16064): Shutting down VM
W/dalvikvm(16064): threadid=1: thread exiting with uncaught exception (group=0x414b12a0)
E/AndroidRuntime(16064): FATAL EXCEPTION: main
E/AndroidRuntime(16064): java.lang.UnsatisfiedLinkError: Native method not found: com.example.hellojni.hellotest.stringFromJNI:()Ljava/
lang/String;
E/AndroidRuntime(16064):        at com.example.hellojni.hellotest.stringFromJNI(Native Method)
E/AndroidRuntime(16064):        at com.example.hellojni.hellotest.onCreate(hellotest.java:38)
E/AndroidRuntime(16064):        at android.app.Activity.performCreate(Activity.java:5206)
E/AndroidRuntime(16064):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
E/AndroidRuntime(16064):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
E/AndroidRuntime(16064):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
E/AndroidRuntime(16064):        at android.app.ActivityThread.access$700(ActivityThread.java:140)
E/AndroidRuntime(16064):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
E/AndroidRuntime(16064):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(16064):        at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(16064):        at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(16064):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16064):        at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(16064):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(16064):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(16064):        at dalvik.system.NativeStart.main(Native Method)

2. 分析:

显然,库没有找到,看似和JNI_OnLoad有关,事实上,这个函数根本没有被调用。这个hello-jni的例子是不需要我们手动去调用JNI_OnLoad的。那么,为什么就是找不到库呢?


3. 解决:

其实,是java代码命名有误,或者说,java的代码命名和c的不匹配。

4. 说明如下:

java代码(这个是正确的):

正确的java代码

package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class HelloJni extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

public native String  stringFromJNI();

 public native String  unimplementedStringFromJNI();

static {
        System.loadLibrary("hello-jni");
    }

}

C代码

#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

没错,就是直接在ndk的sample目录下的例子程序。

下面,贴出错误的java代码:

错误的java代码:

package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
public class helloJniTest extends Activity
{

...

5. 错误原因:

类名与c代码的函数名不匹配。


6. 回顾我的操作:

1. 在ndk下编译出so库;

2. 在eclipse中新建一个android项目,就是这一步导致的错误,因为在新建的时候,包名就很有可能和c代码中的函数名字不匹配,

而使用so库的类名也很容易匹配不上c代码中的函数名。这就是根源所在。

3. 将so库copy在eclipse新建的项目的lib目录下。


我就是犯了第2条错误。


7。总结:C代码一定要与java代码的包名,类名匹配。

8。. 不得不看:

http://blog.csdn.net/liranke/article/details/14223363

http://blog.csdn.net/liranke/article/details/7628593

http://blog.csdn.net/luhuajcdd/article/details/7750146

http://blog.csdn.net/luhuajcdd/article/details/7750151

http://blog.csdn.net/luhuajcdd/article/details/7750158

抱歉!评论已关闭.