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

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

我做错了什么?


当前回答

如果你所有的配置都是正确的,那么试试这个:

adb reverse tcp:8081 tcp:8081

Why?

当RN打包程序正在运行时,在您的浏览器中有一个活动的web服务器,地址为127.0.0.1:8081。应用程序的JS包就是从这个服务器提供的,并在您进行更改时进行刷新。如果没有反向代理,您的手机将无法连接到该地址。

所有功劳归于Swingline0 .

其他回答

从Android 9.0 (API级别28)开始,默认情况下禁用明文支持。

如果您正确地执行正常运行命令,那么您需要这样做来消除这个问题

npm安装 react-native开始 react-native运行android

然后像这样修改你的android manifest文件。

<application
    android:name=".MainApplication"
    android:icon="@mipmap/ic_launcher"
    android:usesCleartextTraffic="true" // add this line with TRUE Value.
android:theme="@style/AppTheme">

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

调试信息,

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

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

netstat -aon | findstr 8081

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

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

react-native run-android --port=9001

确保9001也没有被使用:)

    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

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

我确信它是这里提到的其他东西的某种组合,比如允许明文,这是我为localhost启用的。但这是谜题的最后一块。

project.ext.react = [
    entryFile: "index.js",

    // ADD THESE THREE

    bundleAssetName: "index.android.bundle",
    bundleInDebug: true,
    bundleInRelease: true
]

RN: 0.61.3