Android Release and Publishing to Google Play Store

Tags: android, reactnative, mobile
Published: 2021-05-19 06:20:56

Preparing React Native App for Android Release on Mac
When your React Native Application is ready for release to the Android Platform, you need to perform the following steps to create the .aab file or Android App Bundle which contains your compiled code and resources and you can upload to the Play Store. The APK generation and signing is done by Google Play.

### Generating an upload key

Open Terminal and check the Java Home Path:
``` javascript
/usr/libexec/java_home
```
cd to the path displayed as shown in the image.

Run the keytool command as shown using sudo to generate the keystore and alias as shown below:
``` javascript
sudo keytool -genkey -v -keystore some-helper-key.keystore -alias some-helper-key-alias -keyalg RSA -keysize 2048 -validity 10000
```
As shown in the screenshot, answer the questions that pop and your Keystore will be generated under the Java Home folder.

Copy that Keystore file and paste it in your Android folder under app in your React Native Project.

Modify the android/gradle.properties file and add:
``` javascript
MYAPP_UPLOAD_STORE_FILE=some-helper-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=some-helper-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=***
MYAPP_UPLOAD_KEY_PASSWORD=***
```
You might not want to store the passwords directly in the above configuration and store them in the keychain Access App, then the last 2 password configs are not required.

Add the Release signingconfig section to the android/app/build.gradle file:
``` javascript
signingConfigs {
        debug {
            .....
        }
        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
            }
        }
    }
```
cd to the Android folder and run:
``` javascript
./brew gradlew bundleRelease
```
The .aab file is generated as: android/app/build/outputs/bundle/release/app-release.aab

Now, this is ready to be uploaded to the Play Store.

Issues Faced:

Resource and asset merger: Duplicate resources

The above error occurred for files under drawable folder, app.json, node_modules_reactnativevectoricons_glyphmaps_materialcommunityicons.json, etc.

Go to android/app/src/main/res and delete all the directories starting withdrawable and the above-mentioned .json files. Do not delete the raw folder if it contains any resources.

If you want to directly test the Release version on your device, run the following command under the Root folder of the Project:
``` javascript
react-native run-android --variant=release
```
You can install the bundle tool on Mac:

brew install bundle tool
To extract the APK directly from the .aab file, run the following commands:
``` javascript
bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
```
If you want to test it on a device, then also include the App signing information:
``` javascript
bundletool build-apks --bundle=/MyApp/apprelease.aab --output=/MyApp/my_app.apks
--ks=/MyApp/keystore.jks
--ks-pass=pass:***
--ks-key-alias=MyKeyAlias
--key-pass=pass:***
```
Please note that the .aab file should not contain hyphens else it’ll give a file not found error.

If you want to generate a single Universal apks file, run the below command. Copy the .aab and .keystore files to the folder from where you’ll run bundle tool using terminal:
``` javascript
bundletool build-apks --bundle=MyApp.aab --output=MyApp.apks --mode=universal --ks=my-key.keystore --ks-pass=pass:**** --ks-key-alias=my-key-alias --key-pass=pass:****
```
Change the extension .apks to .zip and extract the universal.apk file. You can rename this file and transfer it to your Android device for installation.

You can also install the .apks directly to your connected device using the bundle tool:
``` javascript
bundletool install-apks --apks=MyApp.apks
```