我正在通过Eclipse Luna + APK19 + ndk_r10b开发Android项目。 我尝试通过一些在线教程获取NDK开发流程。 http://www.decom.ufop.br/imobilis/?p=1832
我能够在Eclipse中编译而没有错误,但是当我运行到虚拟设备时,它弹出错误“java.lang.unsatisfiedlinkerror Native method not found”,然后虚拟设备显示“Unexpected Stop”。我尝试导出为.apk,它也在硬件上失败。我尝试更改不同的教程,但同样的问题出现了。
有助于向我展示一些如何调试的内容吗? 我在这里呆了一个多星期,非常感谢帮助。 谢谢你提前。
09-30 03:07:15.668: I/Process(1723): Sending signal. PID: 1723 SIG: 9
09-30 03:29:02.248: D/dalvikvm(1770): Trying to load lib /data/app-lib/com.test.ndkexample-
2/libNativeFunctionality.so 0xb4d45338
09-30 03:29:02.248: D/dalvikvm(1770): Added shared lib /data/app-lib/com.test.ndkexample 2/libNativeFunctionality.so 0xb4d45338
09-30 03:29:02.248: D/dalvikvm(1770): No JNI_OnLoad found in /data/app-lib/com.test.ndkexample ibNativeFunctionality.so 0xb4d45338, skipping init
09-30 03:29:02.568: W/dalvikvm(1770): No implementation found for native Lcom/test/ndkexample/MainActivity;.HelloWorldNDK:()V
09-30 03:29:02.568: D/AndroidRuntime(1770): Shutting down VM
09-30 03:29:02.568: W/dalvikvm(1770): threadid=1: thread exiting with uncaught exception (group=0xb4a94ba8)
09-30 03:29:02.578: E/AndroidRuntime(1770): FATAL EXCEPTION: main
09-30 03:29:02.578: E/AndroidRuntime(1770): Process: com.test.ndkexample, PID: 1770
09-30 03:29:02.578: E/AndroidRuntime(1770): java.lang.UnsatisfiedLinkError: Native method not found: com.test.ndkexample.MainActivity.HelloWorldNDK:()V
09-30 03:29:02.578: E/AndroidRuntime(1770): at com.test.ndkexample.MainActivity.onStart(MainActivity.java:34)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.Activity.performStart(Activity.java:5241)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.os.Handler.dispatchMessage(Handler.java:102)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.os.Looper.loop(Looper.java:136)
09-30 03:29:02.578: E/AndroidRuntime(1770): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-30 03:29:02.578: E/AndroidRuntime(1770): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 03:29:02.578: E/AndroidRuntime(1770): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 03:29:02.578: E/AndroidRuntime(1770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-30 03:29:02.578: E/AndroidRuntime(1770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-30 03:29:02.578: E/AndroidRuntime(1770): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
package com.test.ndkexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
static {
System.loadLibrary("NativeFunctionality");
}
TextView ndkTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
protected void onStart() {
super.onStart();
HelloWorldNDK();
ndkTextView = (TextView)findViewById(R.id.ndkText);
String ndkString = GetAnswer();
ndkTextView.setText(ndkString);
}
public native void HelloWorldNDK();
public native String GetAnswer();
}
NativeFunctionality.cpp
#include <jni.h>
#include "NativeFunctionality.h"
#include <android/log.h>
#include <pthread.h>
#include <unistd.h>
#define LOG_TAG "NDKTest"
JNIEXPORT void JNICALL Java_com_test_NDKExample_MainActivity_HelloWorldNDK
(JNIEnv *env, jobject obj)
{
__android_log_print(ANDROID_LOG_INFO,LOG_TAG,"Native function called!");
return;
}
JNIEXPORT jstring JNICALL Java_com_test_NDKExample_MainActivity_GetAnswer
(JNIEnv *env, jobject obj)
{
return (env->NewStringUTF("String returned from native environment!"));
}
NativeFunctionality.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_test_ndkexample_MainActivity */
#ifndef _Included_com_test_ndkexample_MainActivity
#define _Included_com_test_ndkexample_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_ndkexample_MainActivity
* Method: HelloWorldNDK
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_test_ndkexample_MainActivity_HelloWorldNDK
(JNIEnv *, jobject);
/*
* Class: com_test_ndkexample_MainActivity
* Method: GetAnswer
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_test_ndkexample_MainActivity_GetAnswer
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE := NativeFunctionality
LOCAL_SRC_FILES := NativeFunctionality.cpp
include $(BUILD_SHARED_LIBRARY)
答案 0 :(得分:1)
JNIEXPORT void JNICALL Java_com_test_NDKExample_MainActivity_HelloWorldNDK(JNIEnv *env, jobject obj)
不正确。您的函数名称必须与您的本机方法声明完全匹配,包括大写(或不包含)包名称。
package com.test.ndkexample;
public class MainActivity extends ActionBarActivity {
public native void HelloWorldNDK();
}
需要
JNIEXPORT void JNICALL Java_com_test_ndkexample_MainActivity_HelloWorldNDK(JNIEnv *env, jobject obj)