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

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

我做错了什么?


当前回答

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

这个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。

其他回答

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

嘿,伙计们,两个按钮的组合解决了我的问题。这与港口有关。

adb reverse tcp:8088 tcp:8088
react-native run-android --port=8088

通过这样做,应用程序可以通过USB连接我的手机。 我认为我的AV或Vagrant或PC中的其他东西正在使用该端口。

如果需要,您可以将8088更改为其他内容。

享受吧!

以下是我在不改变构建的情况下解决这个问题的方法:

1-关闭虚拟设备

2-在物理设备上测试-(它工作)

3-创建新的虚拟设备API 30 Android 10+

4-在新创建的虚拟设备上再次尝试,所有工作都像一个魅力。

在重命名我的android包ID后,我在React Native v0.69中遇到了这个问题。

将/android/app/src/debug/java/old_package_name重命名为 /android/app/src/debug/java/new_package_name,我不小心将AndroidManifest.xml的调试版本移动到新创建的路径中。

在把它移回/android/app/src/debug/后,yarn android又可以正常工作了,没有做任何其他的解决方案。有趣的是,这个错误导致了这个错误,所以如果它发生在其他人身上,检查确保你的文件在他们应该在的地方。

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

调试信息,

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

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

netstat -aon | findstr 8081

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

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

react-native run-android --port=9001

确保9001也没有被使用:)