Skip to main content

发布到 Google Play 商店

Android 要求所有应用在安装之前都必须使用证书进行数字签名。为了通过 谷歌应用商店 分发你的 Android 应用,需要使用发布密钥进行签名,然后该发布密钥需要用于所有未来的更新。自 2017 年以来,借助 Google Play 的应用签名 功能,Google Play 可以自动管理签名版本。但是,在将应用二进制文件上传到 Google Play 之前,需要使用上传密钥对其进行签名。Android 开发者文档中的 签署你的应用 页面详细描述了该主题。本指南简要介绍了该过程,并列出了打包 JavaScript 打包包所需的步骤。

¥Android requires that all apps be digitally signed with a certificate before they can be installed. In order to distribute your Android application via Google Play store it needs to be signed with a release key that then needs to be used for all future updates. Since 2017 it is possible for Google Play to manage signing releases automatically thanks to App Signing by Google Play functionality. However, before your application binary is uploaded to Google Play it needs to be signed with an upload key. The Signing Your Applications page on Android Developers documentation describes the topic in detail. This guide covers the process in brief, as well as lists the steps required to package the JavaScript bundle.

信息

如果你正在使用 Expo,请阅读 部署到应用商店 的 Expo 指南来构建你的应用并将其提交到 Google Play 商店。本指南可与任何 React Native 应用配合使用,以自动化部署过程。

¥If you are using Expo, read the Expo guide for Deploying to App Stores to build and submit your app for the Google Play Store. This guide works with any React Native app to automate the deployment process.

生成上传密钥

¥Generating an upload key

你可以使用 keytool 生成私有签名密钥。

¥You can generate a private signing key using keytool.

Windows

在 Windows keytool 上,必须以管理员身份从 C:\Program Files\Java\jdkx.x.x_x\bin 运行。

¥On Windows keytool must be run from C:\Program Files\Java\jdkx.x.x_x\bin, as administrator.

keytool -genkeypair -v -storetype PKCS12 -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000

此命令会提示你输入密钥库和密钥的密码以及密钥的专有名称字段。然后,它会将密钥库生成为名为 my-upload-key.keystore 的文件。

¥This command prompts you for passwords for the keystore and key and for the Distinguished Name fields for your key. It then generates the keystore as a file called my-upload-key.keystore.

密钥库包含单个密钥,有效期为 10000 天。别名是你稍后在签署应用时将使用的名称,因此请记住记下别名。

¥The keystore contains a single key, valid for 10000 days. The alias is a name that you will use later when signing your app, so remember to take note of the alias.

macOS

在 macOS 上,如果你不确定 JDK bin 文件夹在哪里,请执行以下命令来查找它:

¥On macOS, if you're not sure where your JDK bin folder is, then perform the following command to find it:

/usr/libexec/java_home

它将输出 JDK 的目录,如下所示:

¥It will output the directory of the JDK, which will look something like this:

/Library/Java/JavaVirtualMachines/jdkX.X.X_XXX.jdk/Contents/Home

使用命令 cd /your/jdk/path 导航到该目录,并使用具有 sudo 权限的 keytool 命令,如下所示。

¥Navigate to that directory by using the command cd /your/jdk/path and use the keytool command with sudo permission as shown below.

sudo keytool -genkey -v -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
提醒

请记住保持密钥库文件的私密性。如果你丢失了上传密钥或它已被泄露,你应该 请遵循这些说明

¥Remember to keep the keystore file private. In case you've lost upload key or it's been compromised you should follow these instructions.

设置 Gradle 变量

¥Setting up Gradle variables

  1. my-upload-key.keystore 文件放在项目文件夹中的 android/app 目录下。

    ¥Place the my-upload-key.keystore file under the android/app directory in your project folder.

  2. 编辑文件 ~/.gradle/gradle.propertiesandroid/gradle.properties,并添加以下内容(将 ***** 替换为正确的密钥库密码、别名和密钥密码),

    ¥Edit the file ~/.gradle/gradle.properties or android/gradle.properties, and add the following (replace ***** with the correct keystore password, alias and key password),

MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=*****
MYAPP_UPLOAD_KEY_PASSWORD=*****

这些将是全局 Gradle 变量,我们稍后可以在 Gradle 配置中使用它们来签署我们的应用。

¥These are going to be global Gradle variables, which we can later use in our Gradle config to sign our app.

使用 git 的注意事项

将上述 Gradle 变量保存在 ~/.gradle/gradle.properties 而不是 android/gradle.properties 中可以防止它们被签入 git。你可能必须先在用户的主目录中创建 ~/.gradle/gradle.properties 文件,然后才能添加变量。

¥Saving the above Gradle variables in ~/.gradle/gradle.properties instead of android/gradle.properties prevents them from being checked in to git. You may have to create the ~/.gradle/gradle.properties file in your user's home directory before you can add the variables.

关于安全的注意事项

如果你不热衷于以明文形式存储密码,并且你运行的是 macOS,你也可以 将你的凭据存储在密钥链访问应用中。然后你可以跳过 ~/.gradle/gradle.properties 中的最后两行。

¥If you are not keen on storing your passwords in plaintext, and you are running macOS, you can also store your credentials in the Keychain Access app. Then you can skip the two last rows in ~/.gradle/gradle.properties.

将签名配置添加到应用的 Gradle 配置中

¥Adding signing config to your app's Gradle config

需要完成的最后一个配置步骤是设置要使用上传密钥进行签名的发布版本。编辑项目文件夹中的文件 android/app/build.gradle,并添加签名配置,

¥The last configuration step that needs to be done is to setup release builds to be signed using upload key. Edit the file android/app/build.gradle in your project folder, and add the signing config,

...
android {
...
defaultConfig { ... }
signingConfigs {
release {
if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
storeFile file(MYAPP_UPLOAD_STORE_FILE)
storePassword MYAPP_UPLOAD_STORE_PASSWORD
keyAlias MYAPP_UPLOAD_KEY_ALIAS
keyPassword MYAPP_UPLOAD_KEY_PASSWORD
}
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...

生成发布 AAB

¥Generating the release AAB

在终端中运行以下命令:

¥Run the following command in a terminal:

npx react-native build-android --mode=release

此命令在底层使用 Gradle 的 bundleRelease,将运行应用所需的所有 JavaScript 打包到 AAB (安卓应用包) 中。如果你需要更改 JavaScript 打包包和/或可绘制资源的打包方式(例如,如果你更改了默认文件/文件夹名称或项目的一般结构),请查看 android/app/build.gradle 以了解如何将其更新为 反映这些变化。

¥This command uses Gradle's bundleRelease under the hood that bundles all the JavaScript needed to run your app into the AAB (Android App Bundle). If you need to change the way the JavaScript bundle and/or drawable resources are bundled (e.g. if you changed the default file/folder names or the general structure of the project), have a look at android/app/build.gradle to see how you can update it to reflect these changes.

注意

确保 gradle.properties 不包含 org.gradle.configureondemand=true,因为这将使发布版本跳过将 JS 和资源打包到应用二进制文件中。

¥Make sure gradle.properties does not include org.gradle.configureondemand=true as that will make the release build skip bundling JS and assets into the app binary.

生成的 AAB 可以在 android/app/build/outputs/bundle/release/app-release.aab 下找到,并且可以上传到 Google Play。

¥The generated AAB can be found under android/app/build/outputs/bundle/release/app-release.aab, and is ready to be uploaded to Google Play.

为了让 Google Play 接受 AAB 格式,需要在 Google Play Console 上为你的应用配置 Google Play 的应用签名。如果你要更新不使用 Google Play 应用签名的现有应用,请查看我们的 迁移部分 以了解如何执行该配置更改。

¥In order for Google Play to accept AAB format the App Signing by Google Play needs to be configured for your application on the Google Play Console. If you are updating an existing app that doesn't use App Signing by Google Play, please check our migration section to learn how to perform that configuration change.

测试应用的发布版本

¥Testing the release build of your app

在将发布版本上传到 Play 商店之前,请确保对其进行彻底测试。首先卸载你已安装的应用的任何先前版本。在项目根目录中使用以下命令将其安装在设备上:

¥Before uploading the release build to the Play Store, make sure you test it thoroughly. First uninstall any previous version of the app you already have installed. Install it on the device using the following command in the project root:

npm run android -- --mode="release"

请注意,仅当你按上述方式设置签名时,--mode release 才可用。

¥Note that --mode release is only available if you've set up signing as described above.

你可以终止任何正在运行的打包程序实例,因为所有框架和 JavaScript 代码都打包在 APK 的资源中。

¥You can terminate any running bundler instances, since all your framework and JavaScript code is bundled in the APK's assets.

发布到其他商店

¥Publishing to other stores

默认情况下,生成的 APK 具有 x86x86_64ARMv7aARM64-v8a CPU 架构的原生代码。这使得共享几乎所有 Android 设备上运行的 APK 变得更加容易。然而,这有一个缺点,即任何设备上都会有一些未使用的原生代码,从而导致 APK 不必要地变大。

¥By default, the generated APK has the native code for both x86, x86_64, ARMv7a and ARM64-v8a CPU architectures. This makes it easier to share APKs that run on almost all Android devices. However, this has the downside that there will be some unused native code on any device, leading to unnecessarily bigger APKs.

你可以通过在 android/app/build.gradle 文件中添加以下行来为每个 CPU 创建 APK:

¥You can create an APK for each CPU by adding the following line in your android/app/build.gradle file:

android {

splits {
abi {
reset()
enable true
universalApk false
include "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}

}

将这些文件上传到支持设备定位的市场,例如 亚马逊应用商店F-机器人,用户将自动获得相应的 APK。如果你想上传到其他市场,例如 APK 文件,该市场不支持单个应用的多个 APK,请将 universalApk false 行更改为 true,以创建带有适用于两个 CPU 的二进制文件的默认通用 APK。

¥Upload these files to markets which support device targeting, such as Amazon AppStore or F-Droid, and the users will automatically get the appropriate APK. If you want to upload to other markets, such as APKFiles, which do not support multiple APKs for a single app, change the universalApk false line to true to create the default universal APK with binaries for both CPUs.

请注意,你还必须配置不同的版本代码,例如官方 Android 文档中的 本页建议的

¥Please note that you will also have to configure distinct version codes, as suggested in this page from the official Android documentation.

启用 Proguard 以减小 APK 的大小(可选)

¥Enabling Proguard to reduce the size of the APK (optional)

Proguard 是一个可以稍微减小 APK 大小的工具。它通过剥离你的应用未使用的部分 React Native Java 字节码(及其依赖)来实现此目的。

¥Proguard is a tool that can slightly reduce the size of the APK. It does this by stripping parts of the React Native Java bytecode (and its dependencies) that your app is not using.

重要的

如果你启用了 Proguard,请务必彻底测试你的应用。Proguard 通常需要针对你正在使用的每个原生库进行特定配置。参见 app/proguard-rules.pro

¥Make sure to thoroughly test your app if you've enabled Proguard. Proguard often requires configuration specific to each native library you're using. See app/proguard-rules.pro.

要启用 Proguard,请编辑 android/app/build.gradle

¥To enable Proguard, edit android/app/build.gradle:

/**

* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true

迁移旧的 Android React Native 应用以使用 Google Play 的应用签名

¥Migrating old Android React Native apps to use App Signing by Google Play

如果你从以前版本的 React Native 迁移,你的应用很可能不使用 Google Play 的应用签名功能。我们建议你启用它,以便利用自动应用拆分等功能。为了从旧的签名方式迁移,你需要从 生成新的上传密钥 开始,然后替换 android/app/build.gradle 中的发布签名配置以使用上传密钥而不是发布密钥(请参阅有关 将签名配置添加到 gradle 的部分)。完成后,你应该按照 来自 Google Play 帮助网站的说明 操作,以便将你的原始发行密钥发送到 Google Play。

¥If you are migrating from previous version of React Native chances are your app does not use App Signing by Google Play feature. We recommend you enable that in order to take advantage from things like automatic app splitting. In order to migrate from the old way of signing you need to start by generating new upload key and then replacing release signing config in android/app/build.gradle to use the upload key instead of the release one (see section about adding signing config to gradle). Once that's done you should follow the instructions from Google Play Help website in order to send your original release key to Google Play.

默认权限

¥Default Permissions

默认情况下,INTERNET 权限会添加到你的 Android 应用中,因为几乎所有应用都会使用它。SYSTEM_ALERT_WINDOW 权限会在调试模式下添加到你的 Android APK,但会在生产环境中删除。

¥By default, INTERNET permission is added to your Android app as pretty much all apps use it. SYSTEM_ALERT_WINDOW permission is added to your Android APK in debug mode but it will be removed in production.