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

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

我做错了什么?


当前回答

以下是我在不改变构建的情况下解决这个问题的方法:

1-关闭虚拟设备

2-在物理设备上测试-(它工作)

3-创建新的虚拟设备API 30 Android 10+

4-在新创建的虚拟设备上再次尝试,所有工作都像一个魅力。

其他回答

对我来说,这个问题始于升级react-native。为了添加64位支持,必须进行升级。

Before:
-------- 
Environment:
Node: 10.15.0
npm: 6.9.0
Watchman: 4.9.0
Xcode: Not Found
Android Studio: 3.4 AI-183.6156.11.34.5692245

Packages: (wanted => installed)
react: 16.0.0-alpha.12 => 16.0.0-alpha.12
react-native: ~0.55.2 => 0.55.4
react-native-cli: 2.0.1

After:
------
info 
React Native Environment Info:
Binaries:
Node: 10.15.0
npm: 6.9.0
Watchman: 4.9.0
SDKs:
Android SDK:
API Levels: 23, 26, 27, 28
Build Tools: 27.0.3, 28.0.3
System Images: android-28 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5692245
Xcode: /undefined - /usr/bin/xcodebuild
npmPackages:
react: ^16.8.6 => 16.9.0 
react-native: 0.59.9 => 0.59.9 
npmGlobalPackages:
create-react-native-app: 2.0.2
react-native-cli: 2.0.1

另外,我为升级做了一个重要的改变是在../android/build/build.gradle中

android {
    ...
    defaultConfig {
        ...
        targetSdkVersion 28
        ...
    }
    ...
}

当我试图将构建(.apk)上传到goole play控制台时,我不得不将targetSdkVersion从27更改为28。 我没有意识到这是导致我上述错误的根本原因。@tom和@tinmarfrutos的立即回答是完全有道理的。

我通过添加android:usesCleartextTraffic="true"到我的android/app/src/debug/AndroidManifest.xml解决了这个问题

    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

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

adb reverse tcp:8081 tcp:8081

Why?

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

所有功劳归于Swingline0 .

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

步骤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。

享受:)

如果你在linux上,从App根目录打开终端并运行

npm start

然后打开另一个终端窗口并运行:

react-native run-android