Generate an Android APK and AAB for a React Native App (Secure Way)

Generate an Android APK and AAB for a React Native App (Secure Way)

Generating an APK or Android App Bundle (AAB) for your React Native app is crucial for testing, distribution, and publishing to the Google Play Store. This guide will walk you through creating both an APK and an AAB file using React Native.


Prerequisites

  1. Node.js and npm/yarn installed.

  2. React Native CLI installed.

  3. Android Studio installed and set up.

  4. A React Native project ready for build.


Step 1: Create a Keystore

Run the following command to create a keystore file securely:

keytool -genkey -v -keystore myapp.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias myapp
  • Replace myapp.keystore and myapp with your desired file and alias names.

  • Remember the passwords and file path, as they’ll be required later.


Step 2: Securely Store Keystore Credentials

  1. In your project root, create a new file android/keystore.properties (don’t commit this file to version control).

  2. Add the following content:

storeFile=../android/app/myapp.keystore
storePassword=YourKeystorePassword
keyAlias=myapp
keyPassword=YourKeyPassword
  1. Add keystore.properties to your .gitignore:
android/keystore.properties

Step 3: Update build.gradle Securely

  1. Open android/app/build.gradle and import the properties file at the top:
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()

if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
  1. Configure the signing configs:
android {
    ...
    signingConfigs {
        release {
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

Step 4: Generate APK

To generate an APK, run:

cd android
./gradlew assembleRelease

The APK file will be available in:

android/app/build/outputs/apk/release/app-release.apk

Step 5: Generate AAB (Android App Bundle)

To generate an AAB file, run:

./gradlew bundleRelease

The AAB file will be found at:

android/app/build/outputs/bundle/release/app-release.aab

Step 6: Test the APK/AAB

  1. Transfer the APK to your device or emulator for testing.

  2. Enable “Install from Unknown Sources” in the Android settings.

For AAB testing, use Google Play’s internal testing feature.


Step 7: Publish to Google Play Store

  1. Create a Google Play Developer account.

  2. Upload the AAB file and complete the app listing.

  3. Submit your app for review.


Conclusion

Congratulations! You've successfully built and generated an Android APK for your React Native app. Following these steps will ensure a smooth build process and a ready-to-install APK.