3

我正在尝试制作 NDK 应用程序,但出现此错误:

java.lang.UnsatisfiedLinkError: Native method not found: com.example.hellondk.jni.HelloNDK.hello:()I

看不懂因为C++函数的名字跟Java包名和类一样

你好NDK.cpp

#include <jni.h>

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

你好NDK.java

package com.example.hellondk.jni;

public class HelloNDK {
    public native int hello();

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

安卓.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := HelloNDK
LOCAL_SRC_FILES := HelloNDK.cpp

include $(BUILD_SHARED_LIBRARY)
4

1 回答 1

22

You're exporting it as a C++ function, but the JNI linker doesn't understand C++ name mangling, so it won't be able to find it.

You can use extern "C" to have the function exported without C++ name mangling:

extern "C" JNIEXPORT jint JNICALL Java_com_example_hellondk_jni_HelloNDK_hello(JNIEnv* env, jobject o)
{
    return (jint) 2;
}
于 2013-03-09T15:28:35.767 回答