React Native Android端调用JS模块方法失败问题求助
解决React Native中
.onShouldStartLoadWithRequest()模块未注册错误 错误日志
Invariant Violation: Failed to call into JavaScript module method .onShouldStartLoadWithRequest(). Module has not been registered as callable. Registered callable JavaScript modules (n = 12): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient, AppRegistry, 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
已尝试操作
- 在Android Studio中清理Gradle、清理项目并重新构建
解决方案
1. 排查WebView模块问题
该错误多与WebView的onShouldStartLoadWithRequest回调关联:
- 确保使用维护中的
react-native-webview库,而非React Native已废弃的内置WebView组件 - 确认
react-native-webview已正确安装,RN 0.60+版本支持自动链接,可同步Android Gradle配置验证链接状态
2. 验证JS入口文件与Bundle
- 检查
android/app/src/main/assets/index.android.bundle是否存在,若缺失,手动生成:npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ - 确认
android/app/build.gradle中entryFile配置正确,示例:project.ext.react = [ entryFile: "index.js" ]
3. 彻底清除缓存并重建
- 重置React Native缓存:
npx react-native start --reset-cache - 删除Android构建目录:
rm -rf android/app/build - 重新安装依赖:
yarn install # 或 npm install - 重新编译运行:
npx react-native run-android
4. 检查Hermes引擎配置
- 确认
android/app/build.gradle中Hermes启用配置正确:enableHermes true - 清除Hermes编译缓存:
rm -rf android/app/build/generated/assets/react/release/hermes
5. 排查初始化阶段错误
- 检查入口文件(index.js/App.js)中,
AppRegistry.registerComponent之前的代码是否存在错误(如无效导入、未处理的Promise异常) - 以调试模式运行,查看更早的错误日志:
npx react-native run-android --mode debug
内容的提问来源于stack exchange,提问作者Divy Patel
相关产品推荐
相关产品推荐

