我试图在我的设备上第一次运行我的第一个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

其他回答

对于这个错误:"无法从资产'index.android.bundle'中加载脚本"

1).检查“assets”文件夹:mkdir android\app\src\main\assets

如果文件夹不可用,请手动创建名称为assets的文件夹。并在终端执行Curl命令。

2). Curl命令:Curl "http://localhost:8081/index.android.bundle?android平台=“- o”安卓/ app / src / main /资产/ index.android.bundle”

它将创建“index.android”。Bundle "文件在资产文件夹自动解决了这个问题。

3). react-native run-android

IOS:

在终端中:

cd ios

使用:rm -r build删除build文件夹

再次运行:react-native Run -ios

或者,你可以打开Finder,导航到YOUR_PROJECT/ios并删除构建文件夹。

然后再次运行:react-native run-ios

为ANDROID:

在终端中:

cd android/app

使用:rm -r build删除build文件夹

再次运行:react-native Run -android

或者,你可以打开Finder,导航到YOUR_PROJECT/android/app并删除构建文件夹。

然后再次运行:react-native run-android

就我而言,我忘了Android模拟器的wifi是禁用的。 为了解决这个问题,我只是向下滑动通知栏来展开菜单并启用Wifi连接。

激活Wifi连接后,我的问题就解决了。

我通过这个CMD来解析它:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

在mac中也有同样的问题,在花了2天之后,我终于能够得到它的工作。

由于模拟器蜂窝数据被关闭,我得到了无法从assets index.android.bundle加载脚本,确保你的包正在运行。

确保你的模拟器蜂窝数据是打开的,通过做这个包服务器能够捆绑index.android.js。希望这对那些处于发展阶段的人有所帮助。