Android NDK。找不到本机Lcom的实现

时间:2013-11-14 17:00:59

标签: java android c++ android-ndk java-native-interface

毕竟我已经设法“准备”我的NDK日食(至少我认为如此),它不再在c代码争论,但现在我得到“没有找到本地Lcom的实现”任何时候我执行任何本机代码..它也发生在NDK的样本上..

我的.java

...    
private native float pi(); 
static
{
    System.loadLibrary("shit");
}
...
System.out.println(pi());
...

我的.cpp

#include <jni.h>

jfloat Java_com_example_shit_MainActivity_pi(JNIEnv * env, jobject obj) {
    return 3.1415;
}

我的.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := shit
LOCAL_SRC_FILES := shit.cpp

include $(BUILD_SHARED_LIBRARY)

我的错误日志

11-14 18:30:16.231: D/dalvikvm(1739): Trying to load lib /data/data/com.example.shit/lib/libshit.so 0x413b1fc8
11-14 18:30:16.241: D/dalvikvm(1739): Added shared lib /data/data/com.example.shit/lib/libshit.so 0x413b1fc8
11-14 18:30:16.241: D/dalvikvm(1739): No JNI_OnLoad found in /data/data/com.example.shit/lib/libshit.so 0x413b1fc8, skipping init
11-14 18:30:16.271: D/TextLayoutCache(1739): Using debug level: 0 - Debug Enabled: 0
11-14 18:30:17.913: W/dalvikvm(1739): No implementation found for native Lcom/example/shit/MainActivity;.pi ()F
11-14 18:30:17.913: D/AndroidRuntime(1739): Shutting down VM
11-14 18:30:17.913: W/dalvikvm(1739): threadid=1: thread exiting with uncaught exception (group=0x40ab1210)
11-14 18:30:17.923: E/AndroidRuntime(1739): FATAL EXCEPTION: main
11-14 18:30:17.923: E/AndroidRuntime(1739): java.lang.IllegalStateException: Could not execute method of the activity
...
11-14 18:30:17.923: E/AndroidRuntime(1739):     at dalvik.system.NativeStart.main(Native Method)
...
11-14 18:30:17.923: E/AndroidRuntime(1739): Caused by: java.lang.UnsatisfiedLinkError: pi
11-14 18:30:17.923: E/AndroidRuntime(1739):     at com.example.shit.MainActivity.pi(Native Method)
11-14 18:30:17.923: E/AndroidRuntime(1739):     at com.example.shit.MainActivity.doit(MainActivity.java:31)
...

为什么我得到那个该死的“没有找到原生的实施”???

1 个答案:

答案 0 :(得分:12)

由于您的JNI函数位于C ++源文件中,因此需要将其声明为extern“C”以与JNI兼容。否则,C ++名称修改将妨碍动态链接。

此外,您的本机方法不会返回值。它应该是编译错误。

所以用以下代码替换C函数:

extern "C" {

jfloat Java_com_example_shit_MainActivity_pi(JNIEnv * env, jobject obj) {
    return 3.14;
}

}