因为我是新的反应本机,所以如果有任何错误的步骤,让我知道。

我已经按照文档使用命令构建了一个react原生android应用程序

react-native安卓

在设备上运行时,使用了以下命令

react-native运行android

这给了我2 apk文件在我的projectfolder/android/app/build/outputs/apk的输出

现在,当我安装这个apk安装后,它要求一个开发服务器连接到捆绑JS。但我的要求是,用户不需要与开发服务器作斗争,他只需要安装apk,一切都完成了。

已经通过了一些stackoverflow问题,但对构建不需要开发服务器的unsigned apk没有帮助。

你们能帮我找到如何在react native中构建和unsigned apk的方法吗?


当前回答

在build中为project.ext.react添加devDisabledInDebug: truegradle文件。

其他回答

请按照这些步骤操作。

捆绑你的js:

如果你在根项目中有index.android.js,那么运行

react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

如果你在根项目中有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

创建调试apk:

cd android/
./gradlew assembleDebug

你可以在这里找到你的apk:

cd app/build/outputs/apk/

对我来说,在项目目录中运行以下命令。

对于react原生旧版本(你会在根目录中看到index.android.js):

mkdir -p android/app/src/main/assets && rm -rf android/app/build && react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && cd android && ./gradlew clean assembleRelease && cd ../

对于react原生新版本(你只需要在根目录中看到index.js):

mkdir -p android/app/src/main/assets && rm -rf android/app/build && 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 && cd android && ./gradlew clean assembleRelease && cd ../

apk文件将在以下位置生成:

Gradle < 3.0: android/app/build/outputs/apk/ Gradle 3.0+: android/app/build/outputs/apk/release/

可以生成一个unsigned apk版本用于测试,这样你就可以在你的移动设备上运行。

最初我得到了红屏错误,正如这里提到的。但我遵循了这里提到的同样的方法,它对我很有效。

在控制台的工作目录中,运行这四个命令

react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

cd android

gradlew assembleDebug

gradlew assembleRelease

然后APK文件将在android\app\build\outputs\ APK \debug\app-debug. APK上生成

您可以为此使用两个扩展。这是添加到react-native的设置:

disableDevInDebug: true:在调试buildType中禁用开发服务器 bundleInDebug: true:将jsbundle添加到调试buildType。

最终的project。ext。react在android/app/build中。Gradle应该如下图所示

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
    devDisabledInDev: true, // Disable dev server in dev release
    bundleInDebug: true, // add bundle to dev apk
]

我找到了一个改变buildTypes的解决方案:

buildTypes {
  release {
    signingConfig signingConfigs.release
  }
}