Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptCreate Signed APK/AAB files for React Native App

Create Signed APK/AAB files for React Native App

In this article, you will learn how to create an APK or Android App Bundle (AAB) file for a React Native app. Your application should be digitally signed with an upload key in order to make your Android application published on the Google Play store.

Generating Private Signing Keys in Terminal/cmd: You can use keytool to generate private signing keys. Make sure that keytool must be run from C:\Program Files\Java\jdkx.x.x_x\bin
Open your project location in the terminal/command line as shown in the below picture.

Open your project location in terminal/cmd

Enter this command to generate a Keystore file (not for macOS users):

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

This command will ask Keystore password and some basic information about you. (The password you will enter will not be visible on the screen) 
In the last step, enter “yes” and a 2,048 bit RSA key pair and a self-signed certificate (SHA256withRSA) will be generated with a validity of 10,000 days. This will be stored in the project directory named my-upload-key.keystore.

Output:

my-upload-key.keystore file will be generated in the project directory folder. 

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

If you are using macOS, then use keytool with sudo:

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

Setting up Gradle variables:

Step 1: Move the my-upload-key.keystore file under the android/app directory in the project folder.

Step 2: In the android/gradle.properties, add the following lines as shown below  (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=*****

Note about security: If you are not want to save passwords in plaintext, you can store your credentials in the Keychain Access app. Then you can skip the last two lines in ~/.gradle/gradle.properties.

Add signing configuration to the app’s Gradle configuration

Next, add the signing config in the following lines under android\app\build.gradle in your project folder:-

...
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
        }
    }
}
...

After this step, you can generate APK/AAB file.

Generating the APK:

For APK file generation, run the following command in CMD:

cd android 
./gradlew assembleRelease

Output:

Running this command can take up to 10 minutes.

Now you will get a message BUILD SUCCESSFUL and then an APK file is generated! 
You can find the APK file at android/app/build/outputs/apk/app-release.apk.

Generating the AAB (Optional): For AAB file generation, run the following command in CMD:

cd android
./gradlew bundleRelease

Output:

Running this command can take up to 10 minutes.

You will get a message, BUILD SUCCESSFUL.
Now the AAB file is generated! 
You can find the AAB file at android/app/build/outputs/bundle/release/app.aab which can be uploaded on Google Play Store.

Testing the release build of your app: To test your app before publishing it on Play Store, uninstall any previous version of the app and Install using the command given below:-

npx react-native run-android --variant=release

Optional step: Enabling Proguard to compress APK size.

Proguard tool is used to optimize APK size.

Edit android/app/build.gradle and search for “enableProguardInReleaseBuilds” and make it true to enable proguard :-

def enableProguardInReleaseBuilds = true

RELATED ARTICLES

Most Popular

Recent Comments