在Expo项目中使用aar文件时遭遇UnsatisfiedLinkError问题求助
解决 Expo Android 集成 Movesense mdslib 引发的 UnsatisfiedLinkError 问题
可能的原因分析
- 架构不匹配:mdslib的aar仅包含部分ABI架构,Expo默认打包的架构集合与之冲突,导致运行时找不到对应架构的二进制文件
- Expo构建逻辑差异:不管是托管还是裸机工作流,Expo对原生依赖的打包逻辑和普通RN项目不同,可能未正确将aar中的二进制文件纳入最终APK
- NDK版本兼容问题:mdslib编译使用的NDK版本与Expo项目配置版本差异过大,导致二进制文件无法加载
针对性解决方案
1. 精准控制APK打包的ABI架构
在android/app/build.gradle中明确指定mdslib支持的架构(比如仅支持armeabi-v7a和arm64-v8a):
android { defaultConfig { ndk { abiFilters 'armeabi-v7a', 'arm64-v8a' } } }
同时在app.json/app.config.js中同步配置,避免Expo覆盖设置:
{ "expo": { "android": { "abiFilters": ["armeabi-v7a", "arm64-v8a"] } } }
2. 确保aar文件被正确引入链接
- 裸机工作流:将mdslib的aar文件放在
android/libs目录,在android/app/build.gradle中添加依赖:
dependencies { implementation fileTree(dir: 'libs', include: ['*.aar']) // 其他依赖项 }
- 托管工作流:通过Expo Config Plugin自动配置依赖,在自研的expo-mds插件中添加逻辑:
const { withProjectBuildGradle } = require('@expo/config-plugins'); module.exports = function withMdsLib(config) { return withProjectBuildGradle(config, async (config) => { if (config.modResults.language === 'groovy') { config.modResults.contents += '\ndependencies {\n implementation fileTree(dir: "libs", include: ["*.aar"])\n}\n'; } return config; }); };
3. 对齐NDK版本
找到mdslib编译时使用的NDK版本,在项目中统一配置:
- 在
android/build.gradle中设置:
buildscript { ext { ndkVersion = "xx.x.xxxxxxx" // 替换为mdslib对应的NDK版本 } }
- 同步到
app.json:
{ "expo": { "android": { "ndkVersion": "xx.x.xxxxxxx" } } }
4. 检查原生代码的库加载逻辑
确保加载mdslib的代码中,库名与aar中的二进制文件名完全匹配,比如aar中是libmdslib.so,加载代码应为:
System.loadLibrary("mdslib");
避免拼写错误或多余的前缀/后缀。
验证步骤
- 执行
expo prebuild(裸机)或expo build:android(托管)重新构建项目 - 用
adb logcat查看崩溃日志,确认是否仍存在UnsatisfiedLinkError - 解压生成的APK,检查
lib/目录下是否包含mdslib对应的架构文件夹及so文件
内容的提问来源于stack exchange,提问作者lordspace74
相关产品推荐
相关产品推荐

