【问题标题】:unable to load script from assets 'index.android.bundle'无法从资产“index.android.bundle”加载脚本
【发布时间】:2017-06-20 09:00:10
【问题描述】:

我是 react-native 的新手。 我已经使用“react-native run-android”命令在 Ubuntu 上运行了 react native 项目。我在模拟器上遇到了错误 “无法从资产 'index.android.bundle' 加载脚本。请确保您的包已正确打包或您正在运行包服务器。”

【问题讨论】:

标签: android react-native


【解决方案1】:

我也得到了这个,我在您的项目目录中使用以下命令解决了这个问题:

$ mkdir android/app/src/main/assets

$ react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

$ react-native run-android

【讨论】:

  • 我尝试使用 React Native 版本 3.x 并使用 index.js 更改 index.android.js。
  • 你说的是哪个 react-native 版本。你自己编的?
  • 我有同样的问题,这个解决方案对我不起作用。似乎在我将 babel-plugin-module-resolver 添加到我的项目之后,就会发生这种情况。 (如果它有助于我在我的项目中使用来自 wix 公司的 react-native-navigation)
【解决方案2】:

使用 npm 版本 4.3.0 react-native-cli 版本 2.01 react-native 版本 0.49.5

在项目目录中,

  • mkdir android/app/src/main/assets
  • 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
  • react-native run-android

文件名已从 index.android.js 更改为 index.js

【讨论】:

    【解决方案3】:

    就我而言(将 React Native 作为新的 Activity 嵌入到现有的 Android 代码库中),问题是 Android Studio 自动导入了错误的 BuildConfig

    错误: import com.facebook.react.BuildConfig;

    对: import com.mywebdomain.myapp.BuildConfig;

    这适用于您存放此代码块的任何地方:

    mReactRootView = new ReactRootView(this);
    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setBundleAssetName("index.android.bundle")
            .setJSMainModulePath("index")
            .addPackage(new MainReactPackage())
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    

    【讨论】:

    • 我可以请你喝啤酒吗?
    【解决方案4】:

    这帮助我在以下步骤中解决了问题。

    • 如果不是(在项目目录中)mkdir android/app/src/main/assets

    • react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

    • react-native run-android

    【讨论】:

      【解决方案5】:

      对于这个错误:

      无法从资产“index.android.bundle”加载脚本

      1) 检查“assets”文件夹:

      mkdir android\app\src\main\assets
      

      如果文件夹不可用,请手动创建一个名为“assets”的文件夹。并在终端中执行 Curl 命令。

      2)。卷曲命令:

      curl "http://localhost:8081/index.android.bundle?platform=android" -o"android/app/src/main/assets/index.android.bundle"
      

      它会自动在assets文件夹中创建“index.android.bundle”文件并解决问题。

      3) 那么:

       react-native run-android
      

      【讨论】:

        【解决方案6】:

        Ubuntu

        第一次,我使用react-native init project-name 创建了新应用。我得到了同样的错误。所以我执行以下步骤来解决这个问题。

        1. 在我的例子中首先运行sudo chown user-name-of-pc /dev/kvm
        2. 从您的 Android 手机调试时,选择Use USB to Transfer photos (PTP)
        3. 在“项目名称/android/app/src/main”中创建文件夹资产。
        4. 确保index.js 可用于您的项目root 目录,然后在cd project-name 目录之后从控制台运行以下命令。

        react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/应用程序/src/main/res

        或者对于 index.android.js 然后

        react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

        1. android-studio/bin 目录中运行命令./studio.sh。它将打开 Android Studio。
        2. 运行命令react-native run-android

        【讨论】:

          【解决方案7】:

          这似乎是最新版本的 React Native (0.46) 的问题。使用以前的版本似乎解决了react-native init name --version react-native@0.45.1的问题,并消除了运行react-native run-android时的错误。

          编辑:现在已在 0.46.1 版中修复。

          【讨论】:

            【解决方案8】:

            就我而言,问题出在 React Activity 文件的这一行中:

            mReactInstanceManager = ReactInstanceManager.builder()
            ...
               .setUseDeveloperSupport(BuildConfig.DEBUG)
            ...
            

            BuildConfig.DEBUG 必须设置为 true,而在我的情况下为 false

            【讨论】:

              【解决方案9】:

              仅适用于 Windows 用户:

              1. 复制 adb location 的路径,并在系统变量中设置为PATH
              2. 打开项目结构并删除节点模块文件夹。
              3. 编辑您的项目package.JSON 文件 更改 react-native 版本 "react-native": "0.55.2",和 babel "babel-preset-react-native": "4",之后运行 npm install
              4. 重启 cmd 和 js 服务器并通过 react-native run-android 运行你的 react native 项目

              【讨论】:

                【解决方案10】:

                我被困在同一个问题上几个小时,解决我的问题的是: 在项目的主目录以及“newreactproject\android\app\src\main”中创建“assets”文件夹。然后把这个脚本放到 package.json "android-android": "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 && react-native run-android"
                喜欢:

                "name": "newreactproject",
                  "version": "0.0.1",
                  "private": true,
                  "scripts": {
                    "android-android": "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 && react-native run-android",
                    "start": "node node_modules/react-native/local-cli/cli.js start",
                    "test": "jest"
                  }
                

                【讨论】:

                  【解决方案11】:

                  我在https://github.com/facebook/react-native/issues/15388 的线程中发现了一个建议的解决方案

                  是为手机上的应用手动设置Debug server host & port for device设置。

                  逐步弹出并在 Android 上运行 CRNA

                  在终端:

                  1. create-react-native-app myApp
                  2. cd myApp
                  3. yarn run eject(我使用默认选项“常规 React Native 项目”)
                  4. react-native run-android

                  (现在应该在手机上编译并安装应用程序)

                  在手机上:

                  1. 运行应用程序(预计会看到红色错误屏幕!-单击左下角的关闭按钮)
                  2. 摇晃手机并选择开发设置
                  3. 选择 Debug server host & port for device 并设置为 192.168.x.x:8081(当然是你的实际 LAN IP)
                  4. 在手机上重新启动应用程序,您应该会在顶部看到绿色条“Loading from 192.168.x.x:8081...”
                  5. 您还应该在 Metro Bundler 中看到一些“捆绑 index.js”操作(在运行 react-native run-android 时打开)
                  6. 捆绑包完成后,应用应该会在您的 Android 设备上运行!

                  Live Reload(当源文件更改时)也可以使用 - 只需摇晃手机并触摸“启用 Live Reload”

                  您可以从 Android Studio 运行该项目,但您需要先在 CRNA 项目根目录中使用命令 react-native start 启动 Metro Bundler。

                  【讨论】:

                    【解决方案12】:

                    我也遇到了这个错误。但是adb reverse为我工作

                    【讨论】:

                      【解决方案13】:

                      我使用的是 ubuntu 18.04 LTS,react-native:0.55.2。就我而言,这里有几个解决方案。

                      1. 在你运行 npm run android 之前在终端中输入 npm start。你可能会看到一个错误 错误 Metro Bundler 无法侦听端口 8081。 发生这种情况是因为可能有一个进程正在运行

                        解决方案类型sudo lsof -i :8081

                        你会看到类似这样的输出

                        **COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

                         node    5670 tasif   17u  IPv6  80997      0t0  TCP *:tproxy (LISTEN)**
                        

                        输入kill -9 (PID number)然后输入run npm android

                      2. 如果它仍然不起作用,请再次输入 npm start 。你可能会看到这个错误

                        ERROR ENOSPC: no space left on device, watch'....../......./.....'

                        解决方案:

                        $sudo sysctl fs.inotify.max_user_watches=524288

                        $sudo sysctl -p

                        然后输入npm run android

                        请到此链接查看不同的解决方案和更多详细信息https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

                      3. 删除模拟器并重新构建它。杀死最近的终端,然后构建模拟器并重新启动。

                      【讨论】:

                        猜你喜欢
                        • 2018-07-11
                        • 1970-01-01
                        • 2017-11-10
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        相关资源
                        最近更新 更多