android runtime_error ai_generated true

java.lang.UnsatisfiedLinkError: 未找到 java.lang.String com.example.MyClass.nativeGetString() 的实现(尝试了 Java_com_example_MyClass_nativeGetString 和 Java_com_example_MyClass_nativeGetString__)

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

其他格式: JSON · Markdown 中文 · English
85%修复率
90%置信度
1证据数
2023-05-20首次发现

版本兼容性

版本状态引入弃用备注
NDK r23 - r26 active
CMake 3.22 - 3.28 active
Android Gradle Plugin 7.0 - 8.2 active

根因分析

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

English

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

官方文档

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

解决方案

  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).

无效尝试

常见但无效的做法:

  1. 50% 失败

    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% 失败

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