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

其他回答

如果已经尝试了以上所有解决方案,但仍然遇到错误。 试着和我一样配置你的genymotion:

如果在运行react-native run-android命令时,跟踪的第二行是

JS server not recognized, continuing with build...

这意味着应用程序的打包器无法启动,如果没有一些额外的步骤,应用程序将无法加载。注意,跟踪的结尾仍然报告成功:

BUILD SUCCESSFUL

问题可能是端口冲突(在我的情况下,我完全忘记了默认的IIS站点)。该问题的另一种表现形式是无法在Chrome中打开http://localhost:8081/debugger-ui URL。

一旦端口冲突得到解决,跟踪就会报告

Starting JS server...

然后一个额外的Node窗口会打开(Node…cli.js start),应用程序将成功加载/重新加载。

之后,你应该能够打开调试控制台在Chrome与http://localhost:8081/debugger-ui。

您可以根据官方页面的提示进行修复。这个问题发生在真实设备上,因为JS包位于您的开发系统上,而真实设备中的应用程序不知道它的位置。

这是一个常见的错误消息,您可能在react本机应用程序开发过程中遇到过。所以在本教程中,我们将为这个问题提供解决方案。

问题描述: 无法在windows上从assets index.android.bundle中加载脚本 无法在windows上从assets index.android.bundle中加载脚本

按照以下步骤解决上述问题:

步骤1:在项目目录中创建“assets”文件夹 现在在项目目录中创建资产文件夹,名为“MobileApp\android\app\src\main”。您可以手动创建资产文件夹:

<或>

您也可以使用命令创建文件夹。 命令: mkdir android / app / src / main /资产

步骤2:运行React Native应用程序 让我们运行下面的命令在模拟器或物理设备中运行react本机应用程序。

切换到项目目录。 cd MobileApp

运行下面的命令来帮助捆绑你的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

运行最后一步,在模拟器或物理设备中运行react本机应用程序。 react-native运行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 && react-native run-android

<或>

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

“android-android”:“react-native bundle——platform android——dev false——entry-file index.js——bundle-output android/app/src/main/assets/index.android. js”Bundle——assets-dest android/app/src/main/res && react-native run-android"

如果一切设置正确,您应该很快就会看到新应用程序在Android模拟器中运行。

我的mac上的McAfee阻塞端口8081,不得不将其更改为8082。

首先运行你的包服务器:

react-native start --port 8082

打开另一个终端,像往常一样启动android应用程序:

react-native run-android

一旦它完成,现在重写adb隧道的tcp端口:

adb reverse tcp:8081 tcp:8082

查看adb tcp隧道列表:

adb reverse --list

你现在应该会看到一条温馨的信息:

(reverse) tcp:8081 tcp:8082

回到你的应用程序,重新加载,完成!

PS:不要在应用程序开发设置中更改任何内容,如果你添加了“localhost:8082”,只需删除它,让它为空。

编辑: 对于所有的McAfee受害者,如果你有根访问权限,有一个更简单的解决方案,只是暂时关闭位于端口8081的McAfee进程,根本不需要更改端口:

sudo launchctl remove com.mcafee.agent.macmn