Android Java NDK / JNI - UnsatisfiedLinkError:找不到本机方法[...] Ljava / lang / String;

时间:2013-09-28 18:55:57

标签: android android-ndk java-native-interface unsatisfiedlinkerror

我的程序是一个简单的程序,它使用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 ++方法时,我得到它。 Bellow我已经包含了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)

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:14)

认为Java_com_example_SpotifyWidget_stringFromJNI错误:

尝试:Java_com_example_spotifywidget_MainActivity_stringFromJNI