我對ndk相當陌生,我試圖運行maracana教程啓動時給出的示例。Android NDK示例崩潰:UnsatisfiedLinkError
這裏是我的類定義的方法。
package com.example.fibonacciactivity;
public class FibLib {
// load library .so when load this class
static {
System.loadLibrary("com_example_fibonacciactivity_FibLib");
System.out.println("load library");
}
public native static long fibNR(long n);
public native static long fibNi(long n);
// done lib
}
和我com_example_fibonacciactivity_FibLib.h文件如下:
#include <jni.h>
#ifndef _Included_com_example_fibonacciactivity_FibLib
#define _Included_com_example_fibonacciactivity_FibLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_fibonacciactivity_FibLib
* Method: fibNR
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_example_fibonacciactivity_FibLib_fibNR
(JNIEnv *, jclass, jlong);
/*
* Class: com_example_fibonacciactivity_FibLib
* Method: fibNi
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_example_fibonacciactivity_FibLib_fibNi
(JNIEnv *, jclass, jlong);
#ifdef __cplusplus
}
#endif
#endif
和我com_example_fibonacciactivity_FibLib.c文件的代碼是這樣的:
#include "com_example_fibonacciactivity_FibLib.h"
JNIEXPORT jlong JNICALL Java_com_example_fibonacciactivity_FibLib_fibNR(
JNIEnv *env, jclass jclazz, jlong n) {
return fib(n);
}
JNIEXPORT jlong JNICALL Java_com_example_fibonacciactivity_FibLib_fibNi(
JNIEnv *env, jclass jclazz, jlong n) {
jlong previous = -1;
jlong result = 1;
jlong i;
for (i = 0; i <= s; i++) {
jlong sum = result + previous;
previous = result;
result = sum;
}
return result;
}
static jlong fib(jlong n) {
return n <= 0 ? 0 : n == 1 ? 1 : fib(n - 1) + fib(n - 2);
}
我真的無法弄清楚什麼是問題?任何人都可以幫忙嗎? 我使用在CentOS 6
的Fedora Linux還我得到的錯誤是:
D/dalvikvm(6054): Trying to load lib /data/app-lib/com.example.fibonacciactivity-2/libcom_example_fibonacciactivity_FibLib.so 0x42704a08
D/dalvikvm(6054): Added shared lib /data/app-lib/com.example.fibonacciactivity-2/libcom_example_fibonacciactivity_FibLib.so 0x42704a08
D/dalvikvm(6054): No JNI_OnLoad found in /data/app-lib/com.example.fibonacciactivity-2/libcom_example_fibonacciactivity_FibLib.so 0x42704a08, skipping init
I/System.out(6054): load library
W/dalvikvm(6054): No implementation found for native Lcom/example/fibonacciactivity/FibLib;.fibNR:(J)J
W/dalvikvm(6054): threadid=11: thread exiting with uncaught exception (group=0x41f8eba8)
E/AndroidRuntime(6054): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(6054): Process: com.example.fibonacciactivity, PID: 6054
E/AndroidRuntime(6054): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(6054): at android.os.AsyncTask$3.done(AsyncTask.java:300)
E/AndroidRuntime(6054): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime(6054): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime(6054): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime(6054): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime(6054): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime(6054): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime(6054): at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime(6054): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: com.example.fibonacciactivity.FibLib.fibNR:(J)J
E/AndroidRuntime(6054): at com.example.fibonacciactivity.FibLib.fibNR(Native Method)
E/AndroidRuntime(6054): at com.example.fibonacciactivity.MainActivity$1.doInBackground(MainActivity.java:56)
E/AndroidRuntime(6054): at com.example.fibonacciactivity.MainActivity$1.doInBackground(MainActivity.java:1)
E/AndroidRuntime(6054): at android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime(6054): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime(6054): ... 4 more
W/ActivityManager( 511): Force finishing activity com.example.fibonacciactivity/.MainActivity
我Android.mk是:
3210
爲什麼'#include'沒有被保護? –
triclosan
@triclosan我恐怕我不明白什麼是守護的手段。 ( –
'#ifndef _Included_com_example_fibonacciactivity_FibLib #define _Included_com_example_fibonacciactivity_FibLib'這是防止包含多次的警衛 – triclosan