java.lang.UnsatisfiedLinkError: dlopen 失败:找不到库 "libc++_shared.so"
java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found
ID: android/unsatisfiedlinkerror-libc++-shared-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Android NDK r25c | active | — | — | — |
| Android NDK r26b | active | — | — | — |
| AGP 8.1.0 | active | — | — | — |
| AGP 8.2.2 | active | — | — | — |
| Gradle 8.4 | active | — | — | — |
根因分析
应用使用带有 C++ STL 的原生代码,但未打包共享库 libc++_shared.so 或 APK 打包配置错误。
English
App uses native code with C++ STL but the shared library libc++_shared.so is not bundled or its APK packaging is misconfigured.
官方文档
https://developer.android.com/ndk/guides/cpp-support#shared_runtime解决方案
-
Add `packagingOptions { pickFirst '**/libc++_shared.so' }` in app/build.gradle to ensure the library is included from all dependencies without duplicates. -
Set `android.defaultConfig.ndk.abiFilters 'arm64-v8a', 'x86_64'` to limit native builds to specific ABIs and bundle the correct libc++_shared.so.
无效尝试
常见但无效的做法:
-
70% 失败
Adding android:extractNativeLibs="true" in AndroidManifest.xml may help with extraction but does not fix missing library packaging; the library must be explicitly included.
-
80% 失败
Manually copying libc++_shared.so into jniLibs without matching CPU architectures often leads to UnsatisfiedLinkError for unsupported ABIs.
-
90% 失败
Setting android.useAndroidX=true alone does not resolve native library dependencies; it only affects Java/Kotlin dependencies.