Android调用C ++函数获取UnsatisfiedLinkError

时间:2016-11-09 19:04:07

标签: java android c++

我的native-lib.cpp文件中有以下函数:

JNIEXPORT jlong JNICALL
Java_com_example_z_myapplication_MainActivity_convert32to64(JNIEnv *env, jobject instance,
                                                             jlong l) {

// TODO
l = l + 76561197960265728L;
return l;
}

在我的MainActivity.java中:

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    public static native long convert32to64(long l);

    ...
}

我有一个CMakeList.txt:

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/cpp/native-lib.cpp )        

但是我收到了这个错误:

java.lang.UnsatisfiedLinkError: No implementation found for long com.example.z.myapplication.MainActivity.convert32to64(long) (tried Java_com_example_z_myapplication_MainActivity_convert32to64 and Java_com_example_z_myapplication_MainActivity_convert32to64__J)

谁能告诉我这里有什么问题?

2 个答案:

答案 0 :(得分:2)

C ++ JNI需要extern "C"

extern "C" JNIEXPORT jlong JNICALL
Java_com_example_z_myapplication_MainActivity_convert32to64(
        JNIEnv *env, jobject instance, jlong l) {
    // ...
}

答案 1 :(得分:0)

似乎jni库被错误地构建了。 请检查jni库文件以确认库文件中有确切的符号链接。