未找到本机方法 - JNI Android

时间:2013-12-09 08:49:37

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

我正在尝试使用jni构建一个演示应用程序(我是新手)。我用Google搜索了关于这个问题但没有任何帮助。

以下是我正在使用的代码:

native.c

#include <jni.h>

JNIEXPORT jint JNICALL com_example_MainActivity_helloint(JNIEnv* env, jobject o)
{
    return (jint) 2;
}

MainActivity.java

public class MainActivity extends Activity {

    static {  
        System.loadLibrary("ndk1");  
    }

    public native int helloint();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        helloint();  

    }

}

Android.mk

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_LDLIBS := -llog  
LOCAL_MODULE    := ndk1
LOCAL_SRC_FILES := native.c  
include $(BUILD_SHARED_LIBRARY)

项目构建成功(包括ndk-build)但是当我运行它时,应用程序崩溃并且logcat显示:

12-09 03:38:49.630: W/dalvikvm(2077): No implementation found for native Lcom/example/MainActivity;.helloint:()I
12-09 03:38:49.630: D/AndroidRuntime(2077): Shutting down VM
12-09 03:38:49.640: W/dalvikvm(2077): threadid=1: thread exiting with uncaught exception (group=0xb1ad5b90)
12-09 03:38:49.650: E/AndroidRuntime(2077): FATAL EXCEPTION: main
12-09 03:38:49.650: E/AndroidRuntime(2077): Process: com.example, PID: 2077
12-09 03:38:49.650: E/AndroidRuntime(2077): java.lang.UnsatisfiedLinkError: Native method not found: com.example.MainActivity.helloint:()I
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.example.MainActivity.helloint(Native Method)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.example.MainActivity.onCreate(MainActivity.java:22)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.Activity.performCreate(Activity.java:5243)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.os.Looper.loop(Looper.java:137)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at java.lang.reflect.Method.invoke(Method.java:515)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-09 03:38:49.650: E/AndroidRuntime(2077):     at dalvik.system.NativeStart.main(Native Method)

我不确定我做错了什么。有谁能指出我正确的方向?

感谢您的帮助!!

1 个答案:

答案 0 :(得分:2)

似乎我在功能名称中错过了Java_

JNIEXPORT jint JNICALL Java_com_example_MainActivity_helloint(JNIEnv* env, jobject o)
{
    return (jint) 2;
}

我不确定这是不是真正的问题,但它确实可以解决问题!!