Eclipse在我的基本JNI项目中提供了UnsatisfiedLinkError

时间:2013-10-08 05:36:32

标签: eclipse android-ndk unsatisfiedlinkerror

我对Android的原生方面很陌生。我正在关注Sylvain Ratabouil(PACKT Publishing)的“Android NDK初学者指南”,以了解本地开发的基础知识。我已经在书中遇到了一些问题,并通过Googling找到答案。但似乎没有其他人有这个问题。以下是我的代码

MainActivity.java

package com.jni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

public native String getMyData();
static{
    System.loadLibrary("mylib");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(getMyData());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := mylib
LOCAL_SRC_FILES := com_jni_MainActivity.c

include $(BUILD_SHARED_LIBRARY)

com_jni_MainActivity.c

#include "com_jni_MainActivity.h"

JNIEXPORT jstring Java_com_jni_MainActivity_getMyData(JNIEnv* pEnv, jobject pThis){
    return (*pEnv)->NewStringUTF(pEnv, "Sample Title");
}

com_jni_MainActivity.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_MainActivity */

#ifndef _Included_com_jni_MainActivity
#define _Included_com_jni_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
#undef com_jni_MainActivity_MODE_PRIVATE
#define com_jni_MainActivity_MODE_PRIVATE 0L
#undef com_jni_MainActivity_MODE_WORLD_READABLE
#define com_jni_MainActivity_MODE_WORLD_READABLE 1L
#undef com_jni_MainActivity_MODE_WORLD_WRITEABLE
#define com_jni_MainActivity_MODE_WORLD_WRITEABLE 2L
#undef com_jni_MainActivity_MODE_APPEND
#define com_jni_MainActivity_MODE_APPEND 32768L
#undef com_jni_MainActivity_BIND_AUTO_CREATE
#define com_jni_MainActivity_BIND_AUTO_CREATE 1L
#undef com_jni_MainActivity_BIND_DEBUG_UNBIND
#define com_jni_MainActivity_BIND_DEBUG_UNBIND 2L
#undef com_jni_MainActivity_BIND_NOT_FOREGROUND
#define com_jni_MainActivity_BIND_NOT_FOREGROUND 4L
#undef com_jni_MainActivity_CONTEXT_INCLUDE_CODE
#define com_jni_MainActivity_CONTEXT_INCLUDE_CODE 1L
#undef com_jni_MainActivity_CONTEXT_IGNORE_SECURITY
#define com_jni_MainActivity_CONTEXT_IGNORE_SECURITY 2L
#undef com_jni_MainActivity_CONTEXT_RESTRICTED
#define com_jni_MainActivity_CONTEXT_RESTRICTED 4L
#undef com_jni_MainActivity_RESULT_CANCELED
#define com_jni_MainActivity_RESULT_CANCELED 0L
#undef com_jni_MainActivity_RESULT_OK
#define com_jni_MainActivity_RESULT_OK -1L
#undef com_jni_MainActivity_RESULT_FIRST_USER
#define com_jni_MainActivity_RESULT_FIRST_USER 1L
#undef com_jni_MainActivity_DEFAULT_KEYS_DISABLE
#define com_jni_MainActivity_DEFAULT_KEYS_DISABLE 0L
#undef com_jni_MainActivity_DEFAULT_KEYS_DIALER
#define com_jni_MainActivity_DEFAULT_KEYS_DIALER 1L
#undef com_jni_MainActivity_DEFAULT_KEYS_SHORTCUT
#define com_jni_MainActivity_DEFAULT_KEYS_SHORTCUT 2L
#undef com_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL
#define com_jni_MainActivity_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef com_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL
#define com_jni_MainActivity_DEFAULT_KEYS_SEARCH_GLOBAL 4L
/*
 * Class:     com_jni_MainActivity
 * Method:    getMyData
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_jni_MainActivity_getMyData
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

当我尝试运行程序时,它会崩溃。 logcat提供以下消息

10-08 10:38:36.093: E/AndroidRuntime(31641): FATAL EXCEPTION: main
10-08 10:38:36.093: E/AndroidRuntime(31641): java.lang.UnsatisfiedLinkError: Couldn't load mylib from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.jni-1.apk,libraryPath=/data/app-lib/com.jni-1]: findLibrary returned null
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.System.loadLibrary(System.java:535)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at com.jni.MainActivity.<clinit>(MainActivity.java:11)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.Class.newInstanceImpl(Native Method)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.Class.newInstance(Class.java:1319)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.os.Looper.loop(Looper.java:137)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at android.app.ActivityThread.main(ActivityThread.java:5191)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.reflect.Method.invokeNative(Native Method)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at java.lang.reflect.Method.invoke(Method.java:511)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-08 10:38:36.093: E/AndroidRuntime(31641):    at dalvik.system.NativeStart.main(Native Method)
10-08 10:38:37.882: I/Process(31641): Sending signal. PID: 31641 SIG: 9

libs中有libmylib.so-&gt; armeabi。我不知道我做错了什么

2 个答案:

答案 0 :(得分:1)

检查你的Android.mk,你没有做任何事情来包含com_jni_MainActivity.h头文件。 使用像Local_Includes

这样的东西

答案 1 :(得分:0)

NDK内有许多示例代码。你可以阅读它们并做一些修改来练习。 Android.mk很容易理解。