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

我得到:

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

我使用的命令:

CD(项目目录) react-native开始 react-native运行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

它应该会起作用。

好运!

其他回答

我在真正的android设备上也遇到过同样的问题。

解决方案:基于Niltoid的回答

查找本地IP 打开应用程序,摇晃你的安卓设备 去开发设置和>调试服务器… 粘贴你的IP和端口;X.X.X.X:8088"(其中X是您的本地IP)

Mac用户:打开你的网络首选项,在这里你会得到你的本地IP。

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

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

使用mkdir…/assets && react-native bundle——平台..不能完全解决这个问题,

注意,你必须在每次代码编辑时运行这个命令重新生成文件,并且在重新加载或远程调试JS后再次显示错误。

这个问题的根本原因是你的应用程序无法连接到服务器(因为一些未知的原因)

以下是我的解决方案:

首先,启动其他主机和端口上的服务,例如: React-native start——port 8082——host 0.0.0.0 然后打开应用程序开发菜单> dev Setting >设备的调试服务器主机 输入youripv4address: 8082 dev菜单>重载

希望这能有所帮助

对我来说,gradle清洁修复了它,当我在Android上有问题

cd android

./gradlew clean

操作系统: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

这对我很管用。