Reputation: 13
I am developing Android project thru Eclipse Luna + APK19 + ndk_r10b. I try to pick up NDK development flow thru some online tutorial. http://www.decom.ufop.br/imobilis/?p=1832
I able to compile in Eclipse without error, but when I run to virtual device, it pop-up the Error "java.lang.unsatisfiedlinkerror Native method not found", then the virtual device show "Unexpected Stop". I try to export as .apk, it fail on hardware as well. I try to changes different tutorial, but same problem show up.
Can help to show me some light on how to debug? I have stuck at here more than a week, very appreciate the help. Thanks in advanced.
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)
Upvotes: 1
Views: 1422
Reputation: 40367
JNIEXPORT void JNICALL Java_com_test_NDKExample_MainActivity_HelloWorldNDK(JNIEnv *env, jobject obj)
Is incorrect. Your function name must exactly match your native method declaration, including capitalization (or not) of the package name.
package com.test.ndkexample;
public class MainActivity extends ActionBarActivity {
public native void HelloWorldNDK();
}
requires
JNIEXPORT void JNICALL Java_com_test_ndkexample_MainActivity_HelloWorldNDK(JNIEnv *env, jobject obj)
Upvotes: 1