React-native run-android命令通过在android模拟器中留下消息来终止。信息如下:

无法加载脚本。确保你要么运行Metro服务器,要么运行你的捆绑包index.android。Bundle '被正确地打包以便发布。

我做错了什么?


当前回答

下面这些步骤真的对我很有帮助:

步骤1:在android/app/src/main/assets目录下创建一个目录

Linux命令:mkdir android/app/src/main/assets

第二步:将index.android.js(在根目录下)重命名为index.js(也许有一个index.js文件,在这种情况下你不需要重命名它),然后运行以下命令:

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: 构建APK: react-native run-android

请在最新版本使用index.js。

享受:)

其他回答

类似的问题也发生在我身上。 显然Mcafee阻塞了8081端口。我花了好几个小时才弄明白。

尝试运行:

react-native run-android --port=1234

当应用程序在模拟器上显示错误时,进入开发设置(Ctrl+M)。

将“调试服务器主机和设备端口”更改为“localhost:1234”。

关闭应用程序,并从应用程序抽屉启动它。

试试这个命令

~/Library/Android/sdk/platform-tools/adb -s emulator-5554 reverse tcp:8081 tcp:8081

这也可能是由于在清单中禁用INTERNET权限造成的。我有一个不需要/使用任何互联网的应用程序,我已经删除了它。自从我更新这个应用程序已经有一段时间了,我已经完全忘记了它。花了大约一个小时把每个答案都试了一遍。

(快速回答)

在我的工作空间尝试解决这个问题后,我找到了一个解决方案。

此错误是因为Metro使用NPM和Node版本的某些组合时出现了问题。

你有两个选择:

Alternative 1: Try to update or downgrade npm and node version. Alternative 2: Go to this file: \node_modules\metro-config\src\defaults\blacklist.js and change this code: var sharedBlacklist = [ /node_modules[/\\]react[/\\]dist[/\\].*/, /website\/node_modules\/.*/, /heapCapture\/bundle\.js/, /.*\/__tests__\/.*/ ]; and change to this: var sharedBlacklist = [ /node_modules[\/\\]react[\/\\]dist[\/\\].*/, /website\/node_modules\/.*/, /heapCapture\/bundle\.js/, /.*\/__tests__\/.*/ ]; Please note that if you run an npm install or a yarn install you need to change the code again.

在我的例子中,模拟器的Wifi和移动数据是关闭的。