Recently google mentioned Android requires that all apps need to be digitally signed with a certificate before they can be installed, distributed or upload to Playstore.
In order to generate a React Native executable binary used in Android, we will need a Keystore file which is a Java generated signing key. It can be created by using the keytools in the terminal with the following command:
Generating an upload key
On Windows keytool
must be run from C:\Program Files\Java\jdkx.x.x_x\bin
this is where your java is located.
What is keytool
?
The Java Keytool is a command-line tool which generates public key / private key pairs and stores them in a Java KeyStore. The Keytool executable comes along with the Java SDK (or JRE), so if you have a Java SDK installed you will also have the Keytool executable as it comes with the SDK. The Keytool executable is called keytool.
$ keytool -genkeypair -v -keystore my-upload-key-file.keystore -alias my-key-file-alias -keyalg RSA -keysize 2048 -validity 10000
On Mac/Linux
Navigate to the directory of your jdk path by using the command $ cd /your/jdk/path
.
To know your jdk path run the below command.
/usr/libexec/java_home
And then you can use the keytool command with sudo permission, as shown below.
sudo keytool -genkey -v -keystore my-upload-key-file.keystore -alias my-key-file-alias -keyalg RSA -keysize 2048 -validity 10000
Running the command prompts you for the password of the Keystore, the actual key, with some other distinguished name fields for your key. Therefore, everything should be entered manually and carefully.
Enter your keystore password: password123
Re-enter new password: password123
What is your first and last name? [unknown]: John Doe
What is the name of your organizational unit?: Xyz Company
What is the name of your organization?: Xyz
What is the name of your city or Locality?: test
What is the name of your State or Province?: test
What is the two-letter country code for this unit?: IN
Note: As the keystore
file is required while uploading the app to play store remember to keep it private.
In order to find out the alias name of the generated key, you can run below command.
keytool -keystore release.keystore -list -v
Setting up Gradle variables
- Place the
my-upload-key.keystore
file under theandroid/app
directory in your project folder. - Edit the file
~/.gradle/gradle.properties
orandroid/gradle.properties
, and add the following (replace*****
with the correct Keystore password, alias and key password)
I'd suggest storing set the variables in your ~/.gradle/gradle.properties
as this file is not pushed to a remote repository and its safe
MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-file-alias
MYAPP_UPLOAD_STORE_PASSWORD=*****
MYAPP_UPLOAD_KEY_PASSWORD=*****
These are the global Gradle variables, which can later be used in our Gradle config to sign our app.
Adding signing config to app's Gradle config.
The last step for configuration 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
}
}
}
...
You can have as many build type as you want. Most common are develop
, staging
, release
, etc.
Generating the release APK
Run the following in a terminal:
$ cd android
$ ./gradlew bundleRelease
Gradle's bundleRelease
will bundle all the JavaScript needed to run your app into the AAB (Android App Bundle). If you want to change the way the JavaScript code is bundled and/or drawable resources are bundled (e.g. if you changed the default file/folder names or the general structure of the project), you can check at android/app/build.gradle
to see how you can update it to reflect these changes.
Note: 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.
The generated AAB bundle can be found under android/app/build/outputs/bundle/release/app.aab
, and the app bundle is ready to be uploaded to Google Play.
Difference between using gradlew and gradle:
The difference between gradlew and gradle lies in the fact that ./gradlew indicates you are using a gradle wrapper which is generally part of a project while gradle is part of your system, and it facilitates the installation of gradle. If you were using standalone gradle without the wrapper, you would have to manually install it. In both cases, you are using gradle, but the former is more convenient and ensures version consistency across different machines.
Testing the release build of your app
Before uploading the release apk/bundle build to the Play Store, please make sure you test it thoroughly. Uninstall any previous version of the app which you already have installed. Then, install it on the device using:
$ npx react-native run-android --variant=release
Note that --variant=release
is only available if you've set up signing for variant release as described above.
You may kill any running packager instances as all your framework, and JavaScript code is already bundled in the APK's assets.
Enable Proguard rules to reduce the size of the APK (optional)
Proguard rule is a tool that can slightly reduce the size of the APK. This is achieved by stripping parts of the React Native Java bytecode (and its dependencies) that your react native app is not using.
IMPORTANT: It is important to thoroughly test the app if you've enabled Proguard rule in. Proguard often requires configuration which should be specific to each native library you're using. See app/proguard-rules.pro
.
To enable Proguard, edit android/app/build.gradle
:
/**
* Run Proguard rules to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true
Conclusion
You've successfully generated signed or released apk file for your react-native project. For more detail reference go to Offical React Native docs
💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter here.
Discussions