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

安卓模拟器运行React Native应用报错:无法加载index.android.bundle求助

解决React Native "Unable to load script from assets 'index.android.bundle'" 错误

我之前踩过这个坑,折腾了好一阵才解决,给你列几个实测有效的方案,按顺序试应该能搞定:

  • 确认Metro打包器正在运行
    这个错误最常见的原因就是本地没启动Metro服务器。先看你的终端里有没有运行npx react-native start的窗口,如果没有,在项目根目录执行这个命令,等打包器显示“Loading dependency graph, done.”之后,再重新打开模拟器里的App。

  • 手动生成index.android.bundle文件
    如果不想每次都依赖Metro服务器,或者打包器启动后还是报错,可以手动生成bundle文件:

    1. 先去android/app/src/main目录下,创建一个名为assets的文件夹(如果原本没有的话)
    2. 在项目根目录执行这条打包命令:
      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/
      
    3. 命令执行完后,重新编译运行App:npx react-native run-android
  • 检查AndroidManifest.xml配置
    确保你的App有访问网络的权限,并且debug模式下允许明文流量:
    打开android/app/src/main/AndroidManifest.xml,确认里面有这行权限声明:

    <uses-permission android:name="android.permission.INTERNET" />
    

    同时在<application>标签里加上android:usesCleartextTraffic="true",像这样:

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:usesCleartextTraffic="true" <!-- 加这行 -->
      android:theme="@style/AppTheme">
      ...
    </application>
    

    这个配置是让debug包能正常访问本地的Metro服务器。

  • 清除缓存后重新构建
    有时候旧缓存会搞出奇怪的问题,试试清空所有缓存再重新构建:

    npx react-native clean
    cd android && ./gradlew clean
    cd .. && npx react-native run-android
    

如果以上方法都没效果,试试重启模拟器和你的电脑,有时候环境的小bug重启一下就好了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:43:46