Android NDK C++ JNI (no implementation found for native…)

后端 未结 11 1145
慢半拍i
慢半拍i 2020-11-30 02:02

I\'m trying to use the NDK with C++ and can\'t seem to get the method naming convention correct. my native method is as follows:

extern \"C\" {
JNIEXPORT voi         


        
相关标签:
11条回答
  • 2020-11-30 02:32

    If your package name includes _ character, you should write 1(one) after _ character as shown below:

    MainActivity.java

    package com.example.testcpp_2;
    

    native-lib.cpp

    JNICALL
    Java_com_example_testcpp_12_MainActivity_stringFromJNI(
    
    0 讨论(0)
  • 2020-11-30 02:34

    An additional reason: Use LOCAL_WHOLE_STATIC_LIBRARIES instead of LOCAL_STATIC_LIBRARIES in android.mk. This stops the library from optimizing out unused API calls because the NDK cannot detect the use of the native bindings from java code.

    0 讨论(0)
  • 2020-11-30 02:37

    I Faced the same problem, and in my case the reason was that I had underscore in package name "RFID_Test" I renamed the Package and it worked. Thanks user1222021

    0 讨论(0)
  • 2020-11-30 02:39

    I had the same problem, but to me the error was in the file Android.mk. I had it:

    LOCAL_SRC_FILES := A.cpp
    LOCAL_SRC_FILES := B.cpp 
    

    but should have this:

    LOCAL_SRC_FILES := A.cpp
    LOCAL_SRC_FILES += B.cpp 
    

    note the detail += instead :=

    I hope that helps.

    0 讨论(0)
  • 2020-11-30 02:39

    There is a cpp example under apps in ndk: https://github.com/android/ndk-samples/blob/master/hello-gl2/app/src/main/cpp/gl_code.cpp

    0 讨论(0)
  • 2020-11-30 02:42

    An additional cause for this error: your undecorated native method name must not contain an underscore!

    For example, I wanted to export a C function named AudioCapture_Ping(). Here is my export declaration in C:

    JNI_EXPORT int Java_com_obsidian_mobilehashhost_MainActivity_AudioCapture_Ping(JNIEnv *pJniEnv, jobject object);  //Notice the underscore before Ping
    

    Here was my Java class importing the function:

    package com.obsidian.mobileaudiohashhost;
    ...
    public class MainActivity extends Activity {
        private native int AudioCapture_Ping();  // FAILS
        ...
    

    I could not get Android to dynamically link to my native method until I removed the underscore:

    JNI_EXPORT int Java_com_obsidian_mobilehashhost_MainActivity_AudioCapturePing(JNIEnv *pJniEnv, jobject object); 
    
    package com.obsidian.mobileaudiohashhost;
    ...
    public class MainActivity extends Activity {
        private native int AudioCapturePing();  // THIS WORKS!
        ...
    
    0 讨论(0)
提交回复
热议问题