React Native Generate a release mode APK

Last Updated On October 17, 2021

React Native Generate a release mode APK

 

If you are a react-native developer you may have face the problem how to create a release apk to publish on play store. Let’s see what are the steps that you should follow to do it in correct way in brief. To distribute your Android application via Google Play store, you’ll need to generate a signed release APK.

 

Generating a signing key

You can use keytool to generate a private signing key. If you are on Windows, keytool can be found at C:\Program Files\Java\jdkx.x.x_x\bin . Open a command prompt with Run as administrator or not windows simply run below command on terminal.

keytool -genkey -v -keystore mykeystore.keystore -alias mykeyalias -keyalg RSA -keysize 2048 -validity 10000

Above 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. This 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. After that copy the generated mykeystore.keystore file copy it to android/app directory.

 

Setting up gradle variables

Append the snippet below to android/gradle.properties and add the following (replace ***** with the correct keystore password, alias and key password). These values act as global gradle variables. So we can later use in our gradle config to sign our app.

MYAPP_RELEASE_STORE_FILE=mykeystore.keystore
MYAPP_RELEASE_KEY_ALIAS=mykeyalias
MYAPP_RELEASE_STORE_PASSWORD=*****
MYAPP_RELEASE_KEY_PASSWORD=*****

Adding signing config to your app’s gradle config

Edit android/app/build.gradle so it looks similar to the one below.

...
android {
...
defaultConfig { ... }
signingConfigs {
release {
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
}
}
}
...

Add the siginingConfigs snippet right below but not in defaultConfig and then append signingConfig signingConfigs.release to buildTypes -> release.

 

Generating the release APK

Simply run the below command in terminal and go inside android dierectory in terminal.

cd android 

For windows,

 gradlew assembleRelease

For Linux,

./gradlew assembleRelease

The generated APK can be found under android/app/build/outputs/apk/app-release.apk, and is ready to be distributed.

Before uploading the release build to the Play Store, make sure you test it thoroughly. Install it on the device using:

For windows,

gradlew installRelease

For linux,

./gradlew installRelease

Note that installRelease is only available if you've set up signing as described above.

 

Reducing the size of the APK

You need to enable Proguard which removes the bytecode and dependencies which your app doesn’t use and reducing the size of your APK slightly.

Add the following line under buildTypes in android\app\build.gradle

android {
...
buildTypes {
release {
...
minifyEnabled true
}

}