I’m a newbie with the Android NDK and so I hope my question is not a stupid one.
I followed this tutorial, found on this link:
http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
Everything worked smoothly, I followed every instruction. However, when I ran the application, it’s giving me these errors in my logcat:
07-20 07:35:25.253: D/dalvikvm(390): Trying to load lib /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310
07-20 07:35:25.253: D/dalvikvm(390): Added shared lib /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310
07-20 07:35:25.253: D/dalvikvm(390): No JNI_OnLoad found in /data/data/com.example.testndk/lib/libndkfoo.so 0x40515310, skipping init
07-20 07:35:25.503: W/dalvikvm(390): No implementation found for native Lcom/example/testndk/NdkFooActivity;.invokeNativeFunction ()Ljava/lang/String;
07-20 07:35:25.513: D/AndroidRuntime(390): Shutting down VM
07-20 07:35:25.513: W/dalvikvm(390): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-20 07:35:25.534: E/AndroidRuntime(390): FATAL EXCEPTION: main
07-20 07:35:25.534: E/AndroidRuntime(390): java.lang.UnsatisfiedLinkError: invokeNativeFunction
07-20 07:35:25.534: E/AndroidRuntime(390): at com.example.testndk.NdkFooActivity.invokeNativeFunction(Native Method)
07-20 07:35:25.534: E/AndroidRuntime(390): at com.example.testndk.NdkFooActivity.onCreate(NdkFooActivity.java:23)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.os.Looper.loop(Looper.java:123)
07-20 07:35:25.534: E/AndroidRuntime(390): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-20 07:35:25.534: E/AndroidRuntime(390): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 07:35:25.534: E/AndroidRuntime(390): at java.lang.reflect.Method.invoke(Method.java:507)
07-20 07:35:25.534: E/AndroidRuntime(390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-20 07:35:25.534: E/AndroidRuntime(390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-20 07:35:25.534: E/AndroidRuntime(390): at dalvik.system.NativeStart.main(Native Method)
Does anyone have a slight clue what the issue may be? I’ve tried to figure it out and even started the tutorial from scratch, however the problem persists. Thanks in advance.
My Android java code:
package com.example.testndk;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
public class NdkFooActivity extends Activity {
// load the library - name matches jni/Android.mk
static {
System.loadLibrary("ndkfoo");
}
// declare the native code function - must match ndkfoo.c
private native String invokeNativeFunction();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is where we call the native code
String hello = invokeNativeFunction();
new AlertDialog.Builder(this).setMessage(hello).show();
}
}
C source code:
#include <string.h>
#include <jni.h>
jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
You have
java.lang.UnsatisfiedLinkErrorbecause, may be the c function name didn’t match the fully qualified name of your java class. Check inndkfoo.cto match the package and class name of the target java class:jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis)This should be:
jstring Java_com_example_testndk_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis)or change the package name in your java project to:
package com.mindtherobot.samples.ndkfoo;Then you need to click [Project]->[Clean] before you test so eclipse knows it has to recompile.