我正在研究android-ndk应用程序,我正在使用本机String方法返回字符串值。库编译没有任何错误,但在运行时它给了我一个例外:
java.lang.UnsatisfiedLinkError: Native method not found: com.ziqitza.murgency.activities.FibLib.getTestString:()Ljava/lang/String;
我的C和Header文件代码以及类文件如下所示。
com_testing_ndk_FibLib.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_ziqitza_murgency_activities_FibLib */
#ifndef _Included_com_ziqitza_murgency_activities_FibLib
#define _Included_com_ziqitza_murgency_activities_FibLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_ziqitza_murgency_activities_FibLib
* Method: fibNR
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_ziqitza_murgency_activities_FibLib_fibNR
(JNIEnv *, jclass, jlong);
/*
* Class: com_ziqitza_murgency_activities_FibLib
* Method: fibNI
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_ziqitza_murgency_activities_FibLib_fibNI
(JNIEnv *, jclass, jlong);
/*
* Class: com_ziqitza_murgency_activities_FibLib
* Method: getTestString
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_ziqitza_murgency_activities_FibLib_getTestString
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
FibLib
public class FibLib {
static {
System.loadLibrary("com_ziqitza_murgency_activities_FibLib");
}
public static long fibJR(long n){
return n<=0 ? 0 : n==1 ? 1: fibJR(n - 1) + fibJR(n - 2);
}
public native static long fibNR(long n);
public static long fibJI(long n){
long previous = -1;
long result = 1;
for (long i=0;i<n;i++){
long sum = result + previous;
previous = result;
result = sum;
}
return result;
}
public native static long fibNI(long n);
public native static String getTestString(String abc);
// Test Driver
public static void main(String[] args) {
}
}
C代码
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <com_testing_ndk_FibLib.h>
static jlong fib(jlong n){
return n<=0 ? 0 : n==1 ? 1: fib(n - 1) + fib(n - 2);
}
JNIEXPORT jlong JNICALL Java_com_testing_ndk_FibLib_fibNR
(JNIEnv *env, jclass clazz, jlong n)
{
return fib(n);
}
/*
* Class: com_testing_ndk_FibLib
* Method: fibNI
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_testing_ndk_FibLib_fibNI
(JNIEnv *env, jclass clazz, jlong n)
{
jlong previous = -1;
jlong result = 1;
jlong i;
for (i=0;i<n;i++){
jlong sum = result + previous;
previous = result;
result = sum;
}
return result;
}
JNIEXPORT jstring JNICALL Java_com_testing_ndk_FibLib_getTestString(JNIEnv* env, jclass jclazz) {
jstring str = (*env)->NewStringUTF(env, "Hello from native code!");
return str;
}
答案 0 :(得分:2)
我对NDK并不是很熟悉,但看起来你的C代码和相关的头文件不会使用相同的包名。一个是com_testing_ndk_FibLib
,另一个是com_ziqitza_murgency_activities_FibLib
。