android runtime_error ai_generated true

java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.example.MyClass.nativeGetString() (tried Java_com_example_MyClass_nativeGetString and Java_com_example_MyClass_nativeGetString__)

ID: android/ndk-jni-method-not-found

Also available as: JSON · Markdown · 中文
85%Fix Rate
90%Confidence
1Evidence
2023-05-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
NDK r23 - r26 active
CMake 3.22 - 3.28 active
Android Gradle Plugin 7.0 - 8.2 active

Root Cause

JNI native method signature mismatch: the C/C++ function name does not match the expected mangled name from Java, or the native library is not loaded correctly.

generic

中文

JNI 本地方法签名不匹配:C/C++ 函数名与 Java 期望的修饰名不匹配,或本地库未正确加载。

Official Documentation

https://developer.android.com/ndk/guides/other_build_systems#jni

Workarounds

  1. 90% success Verify the JNI method name by running 'javac -h . MyClass.java' to generate the correct header, then update the C/C++ function signature accordingly. Example: 'JNIEXPORT jstring JNICALL Java_com_example_MyClass_nativeGetString(JNIEnv *env, jobject thiz)'
    Verify the JNI method name by running 'javac -h . MyClass.java' to generate the correct header, then update the C/C++ function signature accordingly. Example: 'JNIEXPORT jstring JNICALL Java_com_example_MyClass_nativeGetString(JNIEnv *env, jobject thiz)'
  2. 80% success Ensure the native library is loaded in a static block: 'static { System.loadLibrary("mylib"); }' and check that the library name matches the .so file (e.g., libmylib.so).
    Ensure the native library is loaded in a static block: 'static { System.loadLibrary("mylib"); }' and check that the library name matches the .so file (e.g., libmylib.so).

中文步骤

  1. Verify the JNI method name by running 'javac -h . MyClass.java' to generate the correct header, then update the C/C++ function signature accordingly. Example: 'JNIEXPORT jstring JNICALL Java_com_example_MyClass_nativeGetString(JNIEnv *env, jobject thiz)'
  2. Ensure the native library is loaded in a static block: 'static { System.loadLibrary("mylib"); }' and check that the library name matches the .so file (e.g., libmylib.so).

Dead Ends

Common approaches that don't work:

  1. 50% fail

    If the library is compiled only for arm64-v8a but the device is armeabi-v7a, the native method won't be found; the ABI must match.

  2. 50% fail

    The JNI naming convention requires exact match; any renaming breaks the linkage.