JNI No implementation found for int com.niubashaoye.simple.jni.JavaTools.addNum(int, int)

 

No implementation found for int com.niubashaoye.simple.jni.JavaTools.addNum(int, int) (tried

 

Java_com_niubashaoye_simple_jni_JavaTools_addNum and Java_com_niubashaoye_simple_jni_JavaTools_addNum__II)

这个错误是JNI时找不到Java_com_niubashaoye_simple_jni_JavaTools_addNum函数

错误1:

                   没有加载so库文件,添加下面代码:

static {
	System.loadLibrary("NDKDemo");
	}

 

错误2:

         jni目录下的cpp文件没有引入头文件,导致jni找不到对应的函数;

    解决方案:cpp文件导入头文件
JNI No implementation found for int com.niubashaoye.simple.jni.JavaTools.addNum(int, int)_第1张图片

 

错误3:

       jni中头文件中的函数在cpp文件的函数名称不对应,解决方案简单把函数名改对了

java的native函数:

 
com.niubashaoye.simple.jni.JavaTools类
public class JavaTools {
	static {
		System.loadLibrary("NDKDemo");
	}

	/**
	 * 调用native函数计算两个数的和
	 * @param num1
	 * @param num2
	 * @return
	 */
	public native int addNum(int num1, int num2);
}
addNum(int num1, int num2);
}

 

jni下的头文件:

 

#include 
/* Header for class com_niubashaoye_simple_jni_JavaTools */


/*
 * Class:     com_niubashaoye_simple_jni_JavaTools
 * Method:    addNum
 * Signature: (II)I
 */                                                                                                                                                                                                  
JNIEXPORT jint JNICALL Java_com_niubashaoye_simple_jni_JavaTools_addNum(
		JNIEnv *, jobject, jint, jint);Java_com_niubashaoye_simple_jni_JavaTools_addNum(
		JNIEnv *, jobject, jint, jint);

 jni目录下的cpp文件:

#include 
#include "com_niubashaoye_simple_jni_JavaTools.h"

JNIEXPORT jint JNICALL Java_com_niubashaoye_simple_jni_JavaTools_addNum2(
		JNIEnv *env, jobject obj, jint num1, jint num2) {
	return num1 + num2;
}
Java_com_niubashaoye_simple_jni_JavaTools_addNum2(
		JNIEnv *env, jobject obj, jint num1, jint num2) {
	return num1 + num2;
}

  注:为了避免这个错误,可以使用javah自动生成头文件,然后cpp文件中直接复制头文件的函数添加{}和参数名;

你可能感兴趣的:(Android错误,NDK入门学习,Android开发)