我试图在我的设备上第一次运行我的第一个React Native项目(Android 4.2.2)。

我得到:

无法从assets index.android.bundle中加载脚本

我使用的命令:

CD(项目目录) react-native开始 react-native运行android


当前回答

1进入你的项目目录,检查这个文件夹是否存在android/app/src/main/assets

如果它存在,那么删除两个文件即index.android.bundle和index.android.bundle.meta 如果文件夹资产不存在,则在那里创建资产目录。

2.从您的根项目目录做

cd android && ./gradlew clean

3.最后,导航回根目录,检查是否有一个名为ledindex.js的条目文件

If there is only one file i.e. index.js then run following command 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 If there are two files i.e index.android.js and index.ios.js then run this 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 Now run react-native run-android

其他回答

我觉得你没有装纱线试试用巧克力之类的东西装。它应该在创建项目之前安装(react-native init命令)。

不需要创建资产目录。

如果不行就回复。

编辑: 在react-native的最新版本中,他们已经修复了这个问题。如果你想完全摆脱这个卸载节点(完全卸载完全删除节点参考这个链接),并重新安装节点,react-native-cli,然后创建你的新项目。

我不是在真正的设备上遇到这个问题,而是在模拟器上遇到的。在尝试使用android studio运行代码之前,我通过在终端中运行react-native start来修复它。在终端中运行react-native run-android时,我从未遇到过这个问题。

首先检查你的assets文件夹是否存在于这个位置\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

然后在android设备上使用

react-native run-android

我在学习React Native教程时也遇到过同样的问题(在Linux上开发,目标是Android)。

这个问题帮助我通过以下步骤解决了问题。

(在项目目录)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运行android

您可以通过将上述步骤放在包的脚本部分来自动化它们。Json是这样的:

"android-linux": "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"

然后你可以每次都从你的命令行执行npm run android-linux。

如果您使用的是Windows,则按照以下方式运行命令,或者如果您得到错误“无法找到入口文件index.android.js”

mkdir android主要\ app \ src \ \资产 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运行android