Reputation: 3139
I found some similar questions, but their answers didn't help me. I went through this tutorial http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
and get some problems with it.
My Android.mk file :
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_PATH)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c
include $(BUILD_SHARED_LIBRARY)
And my c-class ndkfoo.c :
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <string.h>
#include <jni.h>
jstring Java_com_example_ocrrecognise_ndkfoo_MainActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}
and MainActivity:
public class MainActivity extends Activity {
static {
System.loadLibrary("ndkfoo");
}
private native String invokeNativeFunction();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
try{
String hello = invokeNativeFunction();
Log.i("string func", hello);
}catch (UnsatisfiedLinkError e)
{
e.printStackTrace();
}
//new AlertDialog.Builder(MainActivity.this).setMessage(hello).show();
}
});
}
But I've got
No implementation found for native Lcom/example/ocrrecognise/MainActivity;.invokeNativeFunction ()Ljava/lang/String;
java.lang.UnsatisfiedLinkError: invokeNativeFunction
at com.example.ocrrecognise.MainActivity.invokeNativeFunction(Native Method)
at com.example.ocrrecognise.MainActivity.access$0(MainActivity.java:14)
at com.example.ocrrecognise.MainActivity$1.onClick(MainActivity.java:24)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
My in the ndkfoo.c
I put right package name. Help with it please.
Upvotes: 2
Views: 553
Reputation: 463
From the error
No implementation found for native Lcom/example/ocrrecognise/MainActivity;.invokeNativeFunction
it appears that your main activity is in the package com.example.ocrrecognise. If you place it in package com_example_ocrrecognise_ndkfoo, it should work, since that is the prefix that you are using in declaring the function in your native code.
Upvotes: 1
Reputation: 3283
In what package your Activity
is in?
Please mind that is should be under com.example.ocrrecognise.ndkfoo.
How did you compile your c code?
Did you put the compile .so
file under /libs/armeabi
in your project?
Upvotes: 2