我试图在我的设备上第一次运行我的第一个React Native项目(Android 4.2.2)。
我得到:
无法从assets index.android.bundle中加载脚本
我使用的命令:
CD(项目目录) react-native开始 react-native运行android
我试图在我的设备上第一次运行我的第一个React Native项目(Android 4.2.2)。
我得到:
无法从assets index.android.bundle中加载脚本
我使用的命令:
CD(项目目录) react-native开始 react-native运行android
当前回答
同样的问题我已经有好几个月了。我最初使用@Jerry的解决方案,但这是一个错误的解决方案,因为它没有完全解决问题。相反,它所做的是将每个构建都作为产品构建,这意味着你在应用程序中所做的任何微小更改都意味着必须重新构建整个应用程序。
虽然这是一个暂时的解决方案,但从长期来看,它的效果非常糟糕,因为你不再能够利用React Native的惊人的开发工具,如热重载等。
正确的解决方法如下:
在MainApplication.java文件中,替换如下内容:
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
与
@Override
public boolean getUseDeveloperSupport() {
return true;
}
由于某些原因,BuildConfig。DEBUG总是返回false,并在assets目录中生成一个bundle文件。
通过手动将此设置为true,您将强制应用程序使用packager。这将在生产中不起作用,所以一定要将其更改为false或默认值。
别忘了也要跑
$ adb reverse tcp:8081 tcp:8081
其他回答
我在学习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。
使用mkdir…/assets && react-native bundle——平台..不能完全解决这个问题,
注意,你必须在每次代码编辑时运行这个命令重新生成文件,并且在重新加载或远程调试JS后再次显示错误。
这个问题的根本原因是你的应用程序无法连接到服务器(因为一些未知的原因)
以下是我的解决方案:
首先,启动其他主机和端口上的服务,例如: React-native start——port 8082——host 0.0.0.0 然后打开应用程序开发菜单> dev Setting >设备的调试服务器主机 输入youripv4address: 8082 dev菜单>重载
希望这能有所帮助
确保你已经在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上
如果你确定node.js已经成功安装在你的系统上,请检查系统环境变量并在系统变量路径中添加C:\windows\system32
我刚刚遇到了完全相同的问题,并解决了它。希望对大家有所帮助。
假设您正在组装一个发布应用程序包(.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
它应该会起作用。
好运!