从Windows 10切换到Ubuntu 22.04后运行旧React Native项目遇问题
错误根源
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
核心问题是react-native-screens库的原生编译产物未被正确打包进APK,导致App启动时无法加载该库,进而引发后续JS初始化失败。
解决方案步骤
1. 清理项目缓存与冗余构建文件
- 清理Android构建缓存:
cd android && ./gradlew clean rm -rf .gradle build - 清理Node依赖与RN缓存:
cd .. rm -rf node_modules package-lock.json yarn.lock npm install npx react-native start --reset-cache
2. 确保react-native-screens版本与RN兼容
- 检查
package.json中react-native-screens版本,确保与你的React Native版本匹配(例如RN 0.7x需对应react-native-screens@^3.20.0及以上) - 若版本不兼容,重装适配版本:
npm uninstall react-native-screens npm install react-native-screens@<适配版本号>
3. 验证原生链接配置(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/build.gradle的dependencies块是否有:
(自动链接会自动添加,若缺失手动补上)implementation project(':react-native-screens')
4. 重新构建并运行项目
- 执行构建命令:
或直接编译APK:npx react-native run-androidcd android && ./gradlew assembleDebug
5. 额外排查点(若以上步骤无效)
- 确认Ubuntu上的NDK版本符合RN要求(RN 0.7x推荐NDK 25.x)
- 检查
android/local.properties中是否配置了正确的NDK路径:ndk.dir=/path/to/ndk/25.2.9519653
内容的提问来源于stack exchange,提问作者Abdul Hannan

