我找到了一些类似的问题,但是他们的答案对我没有帮助。我浏览了这个教程http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
,并遇到了一些问题。
我的android.mk文件:
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)
和我的C级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!");
}
和主动脉:
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();
}
});
}
但是我有
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)
我在ndkfoo.c
中放置了正确的包装名称。请帮助。
您的Activity
包含哪个软件包?请注意,应该在com.example.ocrrecognise.ndkfoo.
下您是如何编译C代码的?
您是否将编译.so
文件放在/libs/armeabi
下?
来自错误
No implementation found for native Lcom/example/ocrrecognise/MainActivity;.invokeNativeFunction
看来,您的主要活动在com.example.ocrrentise中。如果将其放入包装com_example_ocrrecognise_ndkfoo中,则应该有效,因为那是您在本机代码中声明函数时使用的前缀。