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

我得到:

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

我使用的命令:

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


当前回答

确保metro构建器正在运行,以使物理设备和服务器同步

首先,运行这个命令来运行metro builder

npm开始

然后你可以在react native中开始构建

react-native运行android

他们这次应该不会出错了。如果您希望您的代码在更改时重新加载。摇动你的设备,然后点击“启用Live Reload”

其他回答

IOS:

在终端中:

cd ios

使用:rm -r build删除build文件夹

再次运行:react-native Run -ios

或者,你可以打开Finder,导航到YOUR_PROJECT/ios并删除构建文件夹。

然后再次运行:react-native run-ios

为ANDROID:

在终端中:

cd android/app

使用:rm -r build删除build文件夹

再次运行:react-native Run -android

或者,你可以打开Finder,导航到YOUR_PROJECT/android/app并删除构建文件夹。

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

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

提出一个显而易见的建议,对我很有用。删除应用程序,重新启动服务器,重新从工具部署到设备。

我在学习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。

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

解决方案:基于Niltoid的回答

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

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