No JNI_OnLoad found in /data/data/

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

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值