You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android运行React Native项目报AppRegistry模块未注册错误如何解决

Invariant Violation报错解决方案

Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
中文翻译:不变性违反:AppRegistry模块不是已注册的可调用模块(调用runApplication时触发),该错误的常见原因是应用入口文件路径不正确。

注:已排除watchman、metro基础缓存清理无效的情况,可按以下优先级排查

1. 校验入口文件路径匹配

  • 检查Android项目下app/src/main/java/[你的包名路径]/MainApplication.java中getJSMainModuleName方法的返回值,需和你项目根目录的JS入口文件名完全一致,默认值为index对应index.js,如果你自定义为index.android.js等命名需同步修改此处
  • 检查android/app/build.gradle中project.ext.react配置块的entryFile参数,若显式配置需保证和实际入口文件路径匹配,示例正确配置:
project.ext.react = [
    enableHermes: true,  // 你项目的hermes配置保持不变即可
    entryFile: "index.js"
]

2. 全量清理项目并重新构建

执行以下全量清理命令,避免局部缓存残留:

# 先关闭所有运行中的metro、gradle进程
watchman watch-del-all
rm -rf node_modules
npm install
# 若使用yarn替换为 yarn install
rm -rf /tmp/metro-* /tmp/haste-map-*
rm -rf android/build android/app/build
cd android && ./gradlew clean
cd ..

清理完成后先执行npx react-native start --reset-cache启动metro服务,再新开终端执行npx react-native run-android打包运行。

3. 排查代码与依赖问题

  • 检查根目录index.js代码,确保AppRegistry.registerComponent的第一个参数和app.json中的name字段完全一致,示例写法:
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);
  • 若近期新增了第三方依赖,先回退依赖版本,验证是否是依赖版本和当前RN版本不兼容导致
  • 注释掉近期新增的业务代码,排查是否有未捕获的JS语法错误、模块导入路径错误,这类错误也会触发AppRegistry注册失败

4. 新架构适配排查

如果你开启了RN新架构,可先在android/gradle.properties中将newArchEnabled设置为false,关闭新架构后重新构建验证是否是适配问题导致。

内容的提问来源于stack exchange,提问作者jordanStn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 20:45:03