Android NDK示例崩溃:UnsatisfiedLinkError

时间:2014-02-17 11:36:15

标签: android android-ndk

我是ndk的新手,我正在尝试运行maracana教程中的示例。

这是我定义方法的类。

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是:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILE := com_example_fibonacciactivity_FibLib.c
LOCAL_MODULE := com_example_fibonacciactivity_FibLib
include $(BUILD_SHARED_LIBRARY)

1 个答案:

答案 0 :(得分:3)

问题出在您的Android.mk文件中。变量的名称为LOCAL_SRC_FILES,复数形式。所以只需将第三行修改为:

LOCAL_SRC_FILES := com_example_fibonacciactivity_FibLib.c

你应该没问题(减去一些简单的编译错误)。