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

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

我做错了什么?


当前回答

对我来说,这个问题始于升级react-native。为了添加64位支持,必须进行升级。

Before:
-------- 
Environment:
Node: 10.15.0
npm: 6.9.0
Watchman: 4.9.0
Xcode: Not Found
Android Studio: 3.4 AI-183.6156.11.34.5692245

Packages: (wanted => installed)
react: 16.0.0-alpha.12 => 16.0.0-alpha.12
react-native: ~0.55.2 => 0.55.4
react-native-cli: 2.0.1

After:
------
info 
React Native Environment Info:
Binaries:
Node: 10.15.0
npm: 6.9.0
Watchman: 4.9.0
SDKs:
Android SDK:
API Levels: 23, 26, 27, 28
Build Tools: 27.0.3, 28.0.3
System Images: android-28 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5692245
Xcode: /undefined - /usr/bin/xcodebuild
npmPackages:
react: ^16.8.6 => 16.9.0 
react-native: 0.59.9 => 0.59.9 
npmGlobalPackages:
create-react-native-app: 2.0.2
react-native-cli: 2.0.1

另外,我为升级做了一个重要的改变是在../android/build/build.gradle中

android {
    ...
    defaultConfig {
        ...
        targetSdkVersion 28
        ...
    }
    ...
}

当我试图将构建(.apk)上传到goole play控制台时,我不得不将targetSdkVersion从27更改为28。 我没有意识到这是导致我上述错误的根本原因。@tom和@tinmarfrutos的立即回答是完全有道理的。

我通过添加android:usesCleartextTraffic="true"到我的android/app/src/debug/AndroidManifest.xml解决了这个问题

其他回答

重要, 您的环境中可能有许多Virtual设备。如果您正在更改AVD,请确保再次重复设置。

调试信息,

如果您遇到上述错误,您必须首先验证端口8081上正在运行什么

最快的方法是在终端中使用以下命令

netstat -aon | findstr 8081

如果显示了什么,就意味着端口被阻塞了。如果可能的话,解除那个端口的封锁。

否则,您将需要更改端口。Naveen Kumar在上面的评论中已经提到了这个过程

react-native run-android --port=9001

确保9001也没有被使用:)

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

尝试运行:

react-native run-android --port=1234

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

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

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

首先执行步骤4和5,然后可以运行项目。如果您没有得到结果(步骤4和5),请执行以下步骤

1-尝试降级你的Node版本(当前版本是12.13.1)

  choco uninstall nodejs
  choco install nodejs --version 12.8

2-添加npm模块的路径(C:\Users\your user name\AppData\Roaming\npm)到系统变量而不是用户变量

3-使用命令全局安装react native

  npm install -g react-native-cli

4-进入项目目录的根目录,执行以下命令:

  react-native start

5-打开项目根目录下的另一个终端,执行以下命令:

   react-native run-android 

编辑:

你在用Genymotion ?是,执行以下步骤。

在以上步骤后,如果您得到以下错误?

error Failed to launch emulator. Reason: No emulators found as an output of `emulator -list-avds`.

打开你的genymotion,转到:

genymotion菜单->设置-> ADB ->然后选择使用自定义android sdk工具(点击浏览找到sdk位置)

最后,再次运行您的项目。

对我有效的解决方案如下:

var sharedBlacklist = [
  /node_modules[\/\\]react[\/\\]dist[\/\\].*/,
  /website\/node_modules\/.*/,
  /heapCapture\/bundle\.js/,
  /.*\/__tests__\/.*/
]; 

在我的例子中,我试图在模拟器上运行应用程序。但是,我得到了这个

这个IP 10.0.2.2可以从模拟器chrome浏览器访问。问题是这个IP不在Android网络安全设置白名单中。所以,无论你在这里看到的IP地址添加到下面的设置,你就可以开始了。

./android/app/src/main/AndroidManifest.xml

        <application
                android:name=".MainApplication"
+               android:usesCleartextTraffic="true"   <- Add this line
                android:allowBackup="true"
                android:label="@string/app_name"
                android:icon="@mipmap/ic_launcher"

./android/app/src/main/res/xml/network_security_config.xml
</network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">10.0.1.1</domain>
        <domain includeSubdomains="true">10.0.2.2</domain>
        <domain includeSubdomains="true">10.0.3.2</domain>
    </domain-config>
 </network-security-config>

只需将<domain inclesubdomains ="true">10.0.2.2</domain>替换为react-native错误中显示的IP。