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

我得到:

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

我使用的命令:

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


当前回答

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

其他回答

操作系统:Windows

在尝试以上两种方法后,另一种方法可以解决这个问题 (因为我安装的SDK不在默认位置,如C:\users\"user_name"\appdata\local\ SDK)

如果您注意到命令行显示:“‘adb’未被识别为内部或外部命令……”

这就是我克服的方法: 示例:我在D:\Android\ SDK中安装了SDK 所以我将在系统变量中插入path 2行:

D:\Android\sdk\tools  
D:\Android\sdk\platform-tools  

(如果你不知道如何编辑路径系统变量,这里是主题:https://android.stackexchange.com/questions/38321/what-do-i-type-in-path-variable-for-adb-server-to-start-from-cmd/38324)

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

这对我很管用。

我刚刚遇到了完全相同的问题,并解决了它。希望对大家有所帮助。

假设您正在组装一个发布应用程序包(.apk)

Check if your assets folder exist?: \android\app\src\main\assets If not, create one. After executing "Assemble Release", check if there is anything in that folder(\android\app\src\main\assets) If not, find js bundle file(index.android.bundle), which should be in more than one of the following paths: *a)\android\app\build\intermediates\merged_assets\release\out\index.android.bundle b)\android\app\build\intermediates\merged_assets\release\mergeReleaseAssets\out\index.android.bundle c)\android\app\build\intermediates\assets\release\index.android.bundle d)\android\app\build\generated\assets\react\release\index.android.bundle*

更好的方法是使用“Everything”来搜索文件名:index.android.bundle。

然后将这个文件复制到你的assets文件夹(\android\app\src\main\assets\index.android.bundle) 回到powershell或命令控制台,执行:

React-native run-android——variant=release

它应该会起作用。

好运!

去开发设置->调试服务器主机和端口的设备->输入您的计算机Wifi IP地址(Mac OS:去系统首选项-> Wifi获取Wifi IP地址)

最终重建应用程序并运行

在命令之前使用NPX对我来说是有效的,命令就像下面这样。

npx 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

为什么我们使用npx,这里是npx和npm之间的区别?

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

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

只要加上这一行

android:usesCleartextTraffic="true"

在你的Manifest Application块中。

清单应用程序块代码。

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