NDK(3)java.lang.UnsatisfiedLinkError: Native method not found解决方法

调用native方法时报错如下 : “java.lang.UnsatisfiedLinkError: Native method not found....  ”;

原因分析:

  链接器只看到了在so中该方法声明,没有看到该方法定义.只要让它们匹配就可.

解决方法:

1、检查包名,类名是否出错.

2、c++中的方法Java_xxx_xxx 中的Java 首字母一定要大写.  (这种情况我测试并没有出现)

3、在xxx.cpp中 忘记 include "xxx.h",这是其中一种可能的情况.

  一般javah生成的头文件xxx.h都用extern "C"{...} ,如果这时你在xxx.cpp中实现xxx.h中的方法,那么就要在方法定义前加extern "C" {}

  如果不加extern "C"{} ,编译器生成的是C++类型的函数,而在xxx.h中声明的c函数,相当于只有声明,并没有定义.链接器就找不到.

  要么都编译成c++函数,要么都编译成c函数,

  NativeStudent.cpp 

 1 #include <jni.h>
 2 
 3 #include "com_example_ndksample_NativeStudent.h"
 4 /*
 5 * Class: com_example_ndksample_NativeStudent
 6 * Method: getName
 7 * Signature: ()Ljava/lang/String;
 8 */
 9 JNIEXPORT jstring JNICALL Java_com_example_ndksample_NativeStudent_getName
10 (JNIEnv *env, jobject javaStudent)
11 {
12     return env->NewStringUTF("zhang san");
13 }
14 
15 /*
16  * Class:     com_example_ndksample_NativeStudent
17  * Method:    getCls
18  * Signature: ()Ljava/lang/String;
19  */
20 JNIEXPORT jstring JNICALL Java_com_example_ndksample_NativeStudent_getCls(
21         JNIEnv *env, jclass javaclass)
22 {
23     return env->NewStringUTF("030226");
24 }
25 
26 /*
27  * Class:     com_example_ndksample_NativeStudent
28  * Method:    add
29  * Signature: (II)I
30  */
31 JNIEXPORT jint JNICALL Java_com_example_ndksample_NativeStudent_add(JNIEnv *env,
32         jobject obj, jint x, jint y)
33 {
34     return x + y;
35 }

 

posted @ 2015-08-14 23:14  f9q  阅读(443)  评论(0编辑  收藏  举报