2016-11-09 50 views
0

我已经在我的家乡,lib.cpp文件如下功能:的Android调用C++函数得到UnsatisfiedLinkError

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

对于C++ JNI,您需要extern "C"

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

我刚刚添加了extern“C”,它仍然表示错误 –

+0

@TomDawn是正确重新编译的共享库,它可以在运行时由Java代码访问吗?请注意,Android示例建议在编译一次后注释掉本地lib模块 – nandsito

+0

我只需添加extern“C”并单击run以开始在模拟器中运行。你能告诉我如何重新编译它吗?对不起,也许这是一个愚蠢的问题 –

0

看来jni库的构建是错误的。 请检查jni库文件以确认库文件中有确切的符号链接。

+0

对不起,我对此很陌生,jni库文件在哪里? –