Android Java NDK/JNI - UnsatisfiedLinkError: Native method not found [...] Ljava/lang/String;

Jotunacorn picture Jotunacorn · Sep 28, 2013 · Viewed 13.4k times · Source

My program is intended to be a simple program that uses the Android NDK to implement some c++ code in an android app. The guide i have followed is http://taylorpeer.com/hello-world-cpp-android-ndk/ but when i run the program i get the error

java.lang.UnsatisfiedLinkError: Native method not found: com.example.spotifywidget.MainActivity.stringFromJNI:()Ljava/lang/String;

and i get it when i run the line to call my c++ method via stringFromJNI(). Bellow i have included the 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
    }
}

The 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!");
}

}

the android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := SpotifyWidget
LOCAL_SRC_FILES := SpotifyWidget.cpp
include $(BUILD_SHARED_LIBRARY)

and the logcat output

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)

Thanks in advance for any help :)

Answer

gattshjoty picture gattshjoty · Sep 28, 2013

think Java_com_example_SpotifyWidget_stringFromJNI is wrong:

try: Java_com_example_spotifywidget_MainActivity_stringFromJNI