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

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


当前回答

可以生成一个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 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本地0.55.4,基本上我必须手动捆绑:

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

然后通过usb连接设备,打开usb调试。使用adb设备验证连接的设备。

最后运行react-native run-android,它会在你的手机上安装调试apk,你可以在开发服务器上运行它

注意:

从0.49.0开始,入口点是一个index.js gradlew assemblerrelease只生成不能安装的release-unsigned apks

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

react-native run-android --variant=debug

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

以防其他人最近遇到同样的问题,我使用React Native 0.59.8(也用RN 0.60测试),我可以确认一些其他答案,以下是步骤:

卸载您设备上安装的应用程序的最新编译版本 运行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 assembleDebug命令 在android/app/build/outputs/apk/debug文件夹中获取你的app-debug.apk

好运!

React版本

在根项目目录中

确保你已经有了android/app/src/main/assets/目录,如果没有创建目录,在那之后创建一个新文件并保存为index.android.bundle,然后把你的文件放在这样的android/app/src/main/assets/index.android.bundle中

然后运行这个

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 assembleDebug

然后你可以在app/build/outputs/apk/debug/app-debug.apk中获取apk