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

我得到:

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

我使用的命令:

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


当前回答

使用mkdir…/assets && react-native bundle——平台..不能完全解决这个问题,

注意,你必须在每次代码编辑时运行这个命令重新生成文件,并且在重新加载或远程调试JS后再次显示错误。

这个问题的根本原因是你的应用程序无法连接到服务器(因为一些未知的原因)

以下是我的解决方案:

首先,启动其他主机和端口上的服务,例如: React-native start——port 8082——host 0.0.0.0 然后打开应用程序开发菜单> dev Setting >设备的调试服务器主机 输入youripv4address: 8082 dev菜单>重载

希望这能有所帮助

其他回答

使用npm 4.3.0版本的react-native-cli

在项目目录中,

mkdir android / app / src / main /资产 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

文件名由index.android.js更改为index.js

如果已经尝试了以上所有解决方案,但仍然遇到错误。 试着和我一样配置你的genymotion:

确保你已经在path变量中添加了/path/to/sdk/platform-tools。

当你运行react-native run-android时,它会运行adb reverse tcp:< device-port > tcp:< local-port >命令将请求从你的设备转发到你计算机上本地运行的服务器。 如果没有找到adb,您将看到类似这样的内容。

/bin/sh: 1: adb: not found
Starting the app (.../platform-tools/adb shell am start -n 
com.first_app/com.first_app.MainActivity...
Starting: Intent { cmp=com.first_app/.MainActivity }

对于所有从创建-反应-原生应用程序开发出来的人,在开发过程中遇到这个问题, 这对我来说很管用 这是公认答案中的一个小细节

(在项目目录)mkdir android/app/src/main/assets

下面是需要改变的部分,因为你是在开发中,去掉——dev: false部分:

React-native bundle——platform android——entry-file index.js——bundle-output android / app / src / main /资产/ index.android。包——assets-dest android / app / src / main / res

在关闭所有终端后,从android/app/删除构建文件夹,以确保你开始干净地构建你的应用程序,打开一个新的终端,然后

(在项目目录)NPM运行android

是否会提示打包器控制台(在另一个终端)并成功构建

对于这个问题,我只需要运行:

react-native start

then

adb reverse tcp:8081 tcp:8081

在命令行上。然后我可以运行:

react-native run-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

每一次。