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

我得到:

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

我使用的命令:

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


当前回答

对于这个问题,我只需要运行:

react-native start

then

adb reverse tcp:8081 tcp:8081

在命令行上。然后我可以运行:

react-native run-android

所以我不会浪费时间去跑:

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

每一次。

其他回答

我花了几个小时试图解决这个问题。我的问题不是Windows特有的,而是Android特有的。

在本地和模拟器的浏览器中访问开发服务器。唯一不能工作的是在应用程序中访问开发服务器。

从Android 9.0 (API级别28)开始,默认情况下禁用明文支持。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>

查看更多信息: https://stackoverflow.com/a/50834600/1713216

当我试图调试我的代码时,我有这个问题(在我的android手机和react开发中使用Toggle检查器),我做了以下简单的步骤:

在手机中打开菜单(在重载的地方,我可以通过长按后退键或摇晃手机来打开它) 在这里输入图像描述 点击“开发设置” 在调试->调试服务器主机和设备端口应该为空 注意:如果你在这里写了port或任何东西(用于调试),你应该清除它。

对于这个错误:"无法从资产'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

如果你在你的主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

我在学习React Native教程时也遇到过同样的问题(在Linux上开发,目标是Android)。

这个问题帮助我通过以下步骤解决了问题。

(在项目目录)mkdir android/app/src/main/assets 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 react-native运行android

您可以通过将上述步骤放在包的脚本部分来自动化它们。Json是这样的:

"android-linux": "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 && react-native run-android"

然后你可以每次都从你的命令行执行npm run android-linux。