React-native run-android命令通过在android模拟器中留下消息来终止。信息如下:

无法加载脚本。确保你要么运行Metro服务器,要么运行你的捆绑包index.android。Bundle '被正确地打包以便发布。

我做错了什么?


当前回答

如果你在Linux操作系统上运行,可能会出现npm远程服务器不运行的情况。打开另一个终端(带有项目目录),在执行sudo react-native run-android之前,运行sudo npm start或sudo react-native start命令

其他回答

我也有同样的问题。但是这个方法解决了这个问题。 进入android文件夹使用 CD android然后gradlew clean或。/gradlew clean,以适用于你的操作系统的为准。

下面这些步骤真的对我很有帮助:

步骤1:在android/app/src/main/assets目录下创建一个目录

Linux命令:mkdir android/app/src/main/assets

第二步:将index.android.js(在根目录下)重命名为index.js(也许有一个index.js文件,在这种情况下你不需要重命名它),然后运行以下命令:

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

步骤3: 构建APK: react-native run-android

请在最新版本使用index.js。

享受:)

    this works for me on ubuntu
    
    1. if you are having node version 17 first downgrade its version:-
    You can use n for node's version management.
    
    this is very easy to use.
    $ npm install -g n
    
    then you can show your node version:
    $ node -v
    v16.13.2
    
    2. create an assets folder inside root of the project:-
    project\android\app\src\main\assets
    set folder name should be assets

3. now the final step implement the given code inside the project folder:- 

$cd project 
/project$ 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

4. after following all these step react-native was successfully running on my pc

重要, 您的环境中可能有许多Virtual设备。如果您正在更改AVD,请确保再次重复设置。

调试信息,

如果您遇到上述错误,您必须首先验证端口8081上正在运行什么

最快的方法是在终端中使用以下命令

netstat -aon | findstr 8081

如果显示了什么,就意味着端口被阻塞了。如果可能的话,解除那个端口的封锁。

否则,您将需要更改端口。Naveen Kumar在上面的评论中已经提到了这个过程

react-native run-android --port=9001

确保9001也没有被使用:)

这是由于android设备无法连接到地铁服务器。如果上面的不起作用,检查一下你的android设备是否配置了代理。(设置>代理>不代理)。