我的程序旨在成为一个简单的程序,它使用 Android NDK 在 android 应用程序中实现一些 c++ 代码。我遵循的指南是http://taylorpeer.com/hello-world-cpp-android-ndk/但是当我运行程序时出现错误
java.lang.UnsatisfiedLinkError: Native method not found: com.example.spotifywidget.MainActivity.stringFromJNI:()Ljava/lang/String;
当我通过 stringFromJNI() 运行该行以调用我的 c++ 方法时,我得到了它。下面我已经包含了 MainActivity.java
package com.example.spotifywidget;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
System.err.println("HELLO!");
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.activity_main, menu);
return true;
}
public native String stringFromJNI(); //A string to store the return in
@Override
public void onBackPressed() {
System.err.println(this.stringFromJNI()); //Call the c++ method
}
static{
System.loadLibrary("SpotifyWidget"); //Load the c++ library
}
}
spotifywidget.cpp
#include <jni.h>
#include <string.h>
//#include <api.h>
extern "C"{
JNIEXPORT jstring JNICALL
Java_com_example_SpotifyWidget_stringFromJNI
(JNIEnv *env, jobject obj){
return env->NewStringUTF("Hello from JNI!");
}
}
android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := SpotifyWidget
LOCAL_SRC_FILES := SpotifyWidget.cpp
include $(BUILD_SHARED_LIBRARY)
和 logcat 输出
09-28 18:25:40.176: E/Trace(991): error opening trace file: No such file or directory (2)
09-28 18:25:40.487: D/dalvikvm(991): Trying to load lib /data/data/com.example.spotifywidget/lib/libSpotifyWidget.so 0x41196fc0
09-28 18:25:40.487: D/dalvikvm(991): Added shared lib /data/data/com.example.spotifywidget/lib/libSpotifyWidget.so 0x41196fc0
09-28 18:25:40.496: D/dalvikvm(991): No JNI_OnLoad found in /data/data/com.example.spotifywidget/lib/libSpotifyWidget.so 0x41196fc0, skipping init
09-28 18:25:40.506: W/System.err(991): HELLO!
09-28 18:25:40.766: D/libEGL(991): loaded /system/lib/egl/libEGL_emulation.so
09-28 18:25:40.776: D/(991): HostConnection::get() New Host Connection established 0x2a0e3558, tid 991
09-28 18:25:40.867: D/libEGL(991): loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-28 18:25:40.885: D/libEGL(991): loaded /system/lib/egl/libGLESv2_emulation.so
09-28 18:25:40.986: W/EGL_emulation(991): eglSurfaceAttrib not implemented
09-28 18:25:40.996: D/OpenGLRenderer(991): Enabling debug mode 0
09-28 18:25:44.459: W/dalvikvm(991): No implementation found for native Lcom/example/spotifywidget/MainActivity;.stringFromJNI:()Ljava/lang/String;
09-28 18:25:44.459: D/AndroidRuntime(991): Shutting down VM
09-28 18:25:44.459: W/dalvikvm(991): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-28 18:25:44.466: E/AndroidRuntime(991): FATAL EXCEPTION: main
09-28 18:25:44.466: E/AndroidRuntime(991): java.lang.UnsatisfiedLinkError: Native method not found: com.example.spotifywidget.MainActivity.stringFromJNI:()Ljava/lang/String;
09-28 18:25:44.466: E/AndroidRuntime(991): at com.example.spotifywidget.MainActivity.stringFromJNI(Native Method)
09-28 18:25:44.466: E/AndroidRuntime(991): at com.example.spotifywidget.MainActivity.onBackPressed(MainActivity.java:29)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.app.Activity.onKeyUp(Activity.java:2131)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.app.Activity.dispatchKeyEvent(Activity.java:2361)
09-28 18:25:44.466: E/AndroidRuntime(991): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1819)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3577)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3547)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2797)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.os.Looper.loop(Looper.java:137)
09-28 18:25:44.466: E/AndroidRuntime(991): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-28 18:25:44.466: E/AndroidRuntime(991): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 18:25:44.466: E/AndroidRuntime(991): at java.lang.reflect.Method.invoke(Method.java:511)
09-28 18:25:44.466: E/AndroidRuntime(991): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-28 18:25:44.466: E/AndroidRuntime(991): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 18:25:44.466: E/AndroidRuntime(991): at dalvik.system.NativeStart.main(Native Method)
在此先感谢您的帮助:)