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

我已经按照文档使用命令构建了一个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的方法吗?


当前回答

不使用dev-server生成调试APK

如果您想生成一个调试APK(用于测试),它可以在没有开发服务器的情况下运行。

在你的react原生项目中,去/android/app/src/main创建一个文件夹assets 编辑android >应用> build.gradle

universalApk true  // If true, also generate a universal APK

在react native项目的根目录下运行这个命令

npx 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

现在,进入android文件夹:cd android 并且,运行这个命令:./gradlew assembleDebug (如果构建失败,尝试从android studio构建assembleDebug)

做这些事。

如果你想在设备中安装应用程序,只需运行CMD

adb install 'path/add-debug.apk'

其他回答

不使用dev-server生成调试APK

如果您想生成一个调试APK(用于测试),它可以在没有开发服务器的情况下运行。

在你的react原生项目中,去/android/app/src/main创建一个文件夹assets 编辑android >应用> build.gradle

universalApk true  // If true, also generate a universal APK

在react native项目的根目录下运行这个命令

npx 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

现在,进入android文件夹:cd android 并且,运行这个命令:./gradlew assembleDebug (如果构建失败,尝试从android studio构建assembleDebug)

做这些事。

如果你想在设备中安装应用程序,只需运行CMD

adb install 'path/add-debug.apk'

您需要为调试版本手动创建包。

包调试构建:

#React-Native 0.59
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res

#React-Native 0.49.0+
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

#React-Native 0-0.49.0
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

然后在捆绑后构建APK:

$ cd android
#Create debug build:
$ ./gradlew assembleDebug
#Create release build:
$ ./gradlew assembleRelease #Generated `apk` will be located at `android/app/build/outputs/apk`

附注:另一种方法可能是修改gradle脚本。

遵循第一个响应后,您可以使用

react-native run-android --variant=debug

你的应用程序不需要打包器就能运行

派对已经很晚了,但至少第一页上的解决方案对我来说都没用。因此,我对react注册的年级任务进行了更深入的研究;

只需添加bundleInDebug: true,如@Er Suman G的回答所示

运行。/gradlew assembleDebug或gradle.bat assembleDebug在你的android文件夹根据你的环境。

这将在android/app/build/outputs/apk/debug中生成可调试的非服务器所需的应用程序

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

对于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/