执行npm run android遇librnscreens.so缺失错误,求解决方案
环境
- MacOS + VS Code + Android Studio 模拟器
- React Native 项目,iOS端运行正常
操作与报错
执行 npm run android 时出现以下错误:
ERROR Error: Exception in HostObject::get for prop 'RNSModule': java.lang.UnsatisfiedLinkError: dlopen failed: library "librnscreens.so" not found, js engine: hermes
ERROR Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 11): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient, RCTEventEmitter.
A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
ERROR Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 11): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient, RCTEventEmitter.
A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
已尝试的解决方案
- 修改了
build.gradle配置:
project.ext.react = [ entryFile: "index.js", enableHermes: true, bundleInDebug: true // clean and rebuild if changing ]
- 重置了项目缓存
解决思路
1. 检查react-native-screens依赖配置
- 确认依赖安装:执行
npm list react-native-screens查看版本,未安装则运行npm install react-native-screens --save - 检查自动链接状态(RN 0.60+):查看
android/settings.gradle是否包含以下配置:include ':react-native-screens' project(':react-native-screens').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-screens/android') - 手动注册包(若自动链接失效):打开
android/app/src/main/java/[你的包名]/MainApplication.java,确认getPackages()方法中已添加new ScreensPackage()
2. 彻底清理构建缓存并重新编译
依次执行以下命令:
cd android && ./gradlew clean cd .. && rm -rf node_modules && npm install rm -rf ~/.gradle/caches npm run android
3. 适配模拟器架构
- 部分版本的
react-native-screens可能未编译全架构库,尝试切换模拟器ABI(比如从x86_64切换到armeabi-v7a) - 在Android Studio的AVD管理器中编辑模拟器,重新下载对应ABI的系统镜像后启动测试
4. 排查Hermes兼容性问题
- 暂时将
build.gradle中的enableHermes: true改为false,清理缓存后重新运行。若恢复正常,说明当前react-native-screens与Hermes版本不兼容,尝试升级/降级两者版本
5. 检查最低SDK版本要求
react-native-screens对minSdkVersion有要求,确保android/build.gradle中的minSdkVersion至少为21(部分新版本要求更高,可参考其官方说明)
内容的提问来源于stack exchange,提问作者jhcpadrino

