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

我得到:

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

我使用的命令:

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


当前回答

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-现在工作正常。

希望这对你有所帮助。

其他回答

如果您正在物理设备上运行应用程序并得到此错误

unable to load script from assets index.android.bundle

试着运行命令:

adb reverse tcp:8081 tcp:8081

这对我很管用……

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

如果在运行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。

确保你已经在path变量中添加了/path/to/sdk/platform-tools。

当你运行react-native run-android时,它会运行adb reverse tcp:< device-port > tcp:< local-port >命令将请求从你的设备转发到你计算机上本地运行的服务器。 如果没有找到adb,您将看到类似这样的内容。

/bin/sh: 1: adb: not found
Starting the app (.../platform-tools/adb shell am start -n 
com.first_app/com.first_app.MainActivity...
Starting: Intent { cmp=com.first_app/.MainActivity }

我花了几个小时试图解决这个问题。我的问题不是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