在没有开发服务器的设备上构建和安装unsigned apk?

问题描述 投票:107回答:10

因为我是反应原生的新手,所以如果步骤中有任何错误,请告诉我。

我根据文档使用命令构建了一个react native android应用程序

react-native android

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

react-native run-android

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

enter image description here

现在,当我在安装后使用安装这个apk时,它要求开发服务器连接到捆绑JS。但我的要求是,用户不需要为开发服务器而烦恼,只需要安装apk即可完成所有操作。

已经通过一些stackoverflow Questions但没有帮助构建无需开发服务器的unsigned apk。

你能帮助我找到如何在反应原生中构建和未签名的apk的方法吗?

android apk react-native
10个回答
191
投票

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

捆绑调试版本:

#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脚本。


0
投票

我找到了一个改变buildTypes的解决方案,如下所示:

buildTypes {
  release {
    signingConfig signingConfigs.release
  }
}

-1
投票

更改到android文件夹并运行./gradlew assembleRelease来构建可以发送到Android设备的发布文件。发布apk不需要运行dev服务器。


77
投票

请按照这些步骤操作。

捆绑你的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/

25
投票

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

对于本机旧版本(您将在root中看到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 ../

对于本机新版本(您只需在root中看到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 /

5
投票

按照first response后,您可以使用运行您的应用程序

react-native run-android --variant=debug

并且您的应用程序将运行而无需打包器


4
投票

最近更新

在根项目目录中

确保你已经有目录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


3
投票

对于Windows用户,如果所有步骤都正确遵循:https://facebook.github.io/react-native/docs/signed-apk-android.html

你只需要运行:gradlew assembleRelease

你的文件将是:

  • APP-release.apk
  • 应用程序释放,unaligned.apk

地点:E:\YourProjectName\android\app\build\outputs\apk


2
投票

可以生成未签名的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


1
投票

我正在反应原生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 devices验证连接的设备。

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

注意:

  • 从0.49.0开始,入口点是单个index.js
  • gradlew assembleRelease只生成无法安装的release-unsigned apks

1
投票

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

  1. 卸载已安装在设备上的应用程序的最新编译版本
  2. 运行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. 运行cd android/ && ./gradlew assembleDebug
  4. 获取app-debug.apk文件夹android / app / build / outputs / apk / debug

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.