react-native/docs/SignedAPKAndroid.md

114 lines
4.9 KiB
Markdown
Raw Normal View History

---
id: signed-apk-android
title: Generating Signed APK
layout: docs
category: Guides (Android)
permalink: docs/signed-apk-android.html
banner: ejected
next: android-building-from-source
previous: headless-js-android
---
Android requires that all apps be digitally signed with a certificate before they can be installed, so to distribute your Android application via [Google Play store](https://play.google.com/store), you'll need to generate a signed release APK. The [Signing Your Applications](https://developer.android.com/tools/publishing/app-signing.html) 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 packaging the JavaScript bundle.
### Generating a signing key
You can generate a private signing key using `keytool`. On Windows `keytool` must be run from `C:\Program Files\Java\jdkx.x.x_x\bin`.
2015-10-09 14:37:06 +00:00
$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
2015-09-26 18:33:41 +00:00
This command prompts you for passwords for the keystore and key, and to provide the Distinguished Name fields for your key. It then generates the keystore as a file called `my-release-key.keystore`.
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.
_Note: Remember to keep your keystore file private and never commit it to version control._
### Setting up gradle variables
1. Place the `my-release-key.keystore` file under the `android/app` directory in your project folder.
2015-09-26 18:33:41 +00:00
2. Edit the file `~/.gradle/gradle.properties` and add the following (replace `*****` with the correct keystore password, alias and key password),
```
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
2015-10-09 14:37:06 +00:00
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****
```
These are going to be global gradle variables, which we can later use in our gradle config to sign our app.
> __Note about saving the keystore:__
> Once you publish the app on the Play Store, you will need to republish your app under a different package name (losing all downloads and ratings) if you want to change the signing key at any point. So backup your keystore and don't forget the passwords.
_Note about security: If you are not keen on storing your passwords in plaintext and you are running OSX, you can also [store your credentials in the Keychain Access app](https://pilloxa.gitlab.io/posts/safer-passwords-in-gradle/). Then you can skip the two last rows in `~/.gradle/gradle.properties`._
### Adding signing config to your app's gradle config
Edit the file `android/app/build.gradle` in your project folder and add the signing config,
2015-11-29 04:43:06 +00:00
```gradle
...
android {
...
defaultConfig { ... }
signingConfigs {
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...
```
2015-10-09 16:00:12 +00:00
### Generating the release APK
2015-10-22 18:11:00 +00:00
Simply run the following in a terminal:
```sh
$ cd android && ./gradlew assembleRelease
```
Gradle's `assembleRelease` will bundle all the JavaScript needed to run your app into the APK. 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.
2015-10-22 18:11:00 +00:00
The generated APK can be found under `android/app/build/outputs/apk/app-release.apk`, and is ready to be distributed.
2015-10-09 15:55:07 +00:00
2015-10-09 16:00:12 +00:00
### Testing the release build of your app
2015-10-09 15:55:07 +00:00
2015-10-09 16:00:12 +00:00
Before uploading the release build to the Play Store, make sure you test it thoroughly. Install it on the device using:
2015-10-09 15:55:07 +00:00
```sh
$ react-native run-android --variant=release
2015-10-09 15:55:07 +00:00
```
Note that `--variant=release` is only available if you've set up signing as described above.
2015-10-09 15:55:07 +00:00
Corrected a minor error Summary: Not sure if that is how it's supposed to read but tried to correct it Thanks for submitting a PR! Please read these instructions carefully: - [ ] Explain the **motivation** for making this change. - [x] Provide a **test plan** demonstrating that the code is solid. - [x] Match the **code formatting** of the rest of the codebase. - [x] Target the `master` branch, NOT a "stable" branch. What existing problem does the pull request solve? A good test plan has the exact commands you ran and their output, provides screenshots or videos if the pull request changes UI or updates the website. See [What is a Test Plan?][1] to learn more. If you have added code that should be tested, add tests. Sign the [CLA][2], if you haven't already. Small pull requests are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it. Make sure all **tests pass** on both [Travis][3] and [Circle CI][4]. PRs that break tests are unlikely to be merged. For more info, see the ["Pull Requests"][5] section of our "Contributing" guidelines. [1]: https://medium.com/martinkonicek/what-is-a-test-plan-8bfc840ec171#.y9lcuqqi9 [2]: https://code.facebook.com/cla [3]: https://travis-ci.org/facebook/react-native [4]: http://circleci.com/gh/facebook/react-native [5]: https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#pull-requests Closes https://github.com/facebook/react-native/pull/14085 Differential Revision: D5103609 Pulled By: javache fbshipit-source-id: 483661b149761cf6d27fb43990272312d781cc6f
2017-05-22 13:56:02 +00:00
You can kill any running packager instances, all your framework and JavaScript code is bundled in the APK's assets.
2015-10-09 15:55:07 +00:00
### Enabling Proguard to reduce the size of the APK (optional)
2015-10-09 16:11:25 +00:00
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.
2015-10-09 15:55:07 +00:00
2015-10-09 16:12:05 +00:00
_**IMPORTANT**: 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`._
2015-10-09 15:55:07 +00:00
To enable Proguard, edit `android/app/build.gradle`:
2015-10-09 15:55:07 +00:00
2015-11-29 04:43:06 +00:00
```gradle
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true
2015-10-09 15:55:07 +00:00
```