2016-11-09 69 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庫文件在哪裏? –