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

我得到:

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

我使用的命令:

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


当前回答

如果你在你的主AndroidManifest.xml中定义了android:networkSecurityConfig,这个问题也会发生。android:usesCleartextTraffic="true"在你的调试AndroidManifest.xml将被忽略。

要解决这个问题,你必须配置应用程序允许http流量到localhost。

创建一个文件android/src/debug/res/xml/network_security_config.xml:

<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

并在android/src/debug/AndroidManifest.xml中引用它:

<?xml version="1.0" encoding="utf-8" ?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application 
        android:networkSecurityConfig="@xml/network_security_config" 
        tools:targetApi="28" 
        tools:ignore="GoogleAppIndexingWarning" />
</manifest>

更多信息请访问:https://medium.com/astrocoders/i-upgraded-to-android-p-and-my-react-native-wont-connect-to-my-computer-to-download-index-delta-42580377e1d3

其他回答

确保metro构建器正在运行,以使物理设备和服务器同步

首先,运行这个命令来运行metro builder

npm开始

然后你可以在react native中开始构建

react-native运行android

他们这次应该不会出错了。如果您希望您的代码在更改时重新加载。摇动你的设备,然后点击“启用Live Reload”

对我来说,gradle清洁修复了它,当我在Android上有问题

cd android

./gradlew clean

此问题也可能是由于设备/模拟器的蜂窝数据被关闭引起的。一定要检查一下。

The most upVoted answer seems working but become hectic for developers to see their minor changes after couple of minutes. I solved this issue by following these steps: 1- Push my code to bitbucket/github repo ( to view the changes later ) 2- cut paste android folder to any safe location for recovery 3- delete the android folder from my project 4- run react-native upgrade 5- Now, I see all the changes between the previous and the lastest build (using VsCode) 6- I found the issue in build.gradle file in these lines of code.

ext {
buildToolsVersion = "28.0.2"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 27
supportLibVersion = "28.0.0"
}

7-我复制了这些行(由react-native升级脚本生成,并再次删除android文件夹。 8-然后我剪切粘贴android文件夹回到我的项目文件夹,我在第2步剪切粘贴。 9 -替换构建。Gradle文件行与复制的。 10-现在工作正常。

希望这对你有所帮助。

我花了半天时间来解决这个问题。

如果你使用的是28.0.0以上的API目标版本,那么你可能会遇到这个问题。

只要加上这一行

android:usesCleartextTraffic="true"

在你的Manifest Application块中。

清单应用程序块代码。

 <application
        ....
        android:usesCleartextTraffic="true"
.../>