React Native APK and AAB Generation Guide
This document outlines the steps to generate APK and AAB files for React Native applications, including enabling Proguard to reduce APK size.
1. Generate a Keystore for Signing
To sign your app, you need a keystore. Use the following command:
keytool -genkeypair -v -storetype PKCS12 -keystore my-upload-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
Example Information:
· Keystore Name: my-upload-key.keystore
· Alias Name: my-key-alias
· Store Password: Replace with your secure password.
· Key Password: Replace with your secure password.
Environment Variables Configuration
Add the following environment variables to your project:
MYAPP_UPLOAD_STORE_FILE=my-upload-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=my-key-alias
MYAPP_UPLOAD_STORE_PASSWORD=*****
MYAPP_UPLOAD_KEY_PASSWORD=*****
Replace ***** with your actual passwords.
2. Configure build.gradle
Edit the android/app/build.gradle file to include signing configurations:
android {
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
}
}
}
3. Generate the Release APK and AAB
For APK File
Run the following command to build the APK:
./gradlew assembleDebug
For AAB File
Run the following command to build the AAB:
npx react-native build-android — mode=release
Combined Command for Both AAB and APK Files
To build both AAB and APK files, modify the script:
cd android
./gradlew bundleRelease assembleRelease
Artifacts Location:
· AAB: android/app/build/outputs/**/*.aab
· APK: android/app/build/outputs/apk/release/*.apk
4. Example Build Script
scripts:
- name: Install npm dependencies
script: |
npm install
- name: Set Android SDK location
script: |
echo “sdk.dir=$ANDROID_SDK_ROOT” > “$CM_BUILD_DIR/android/local.properties”
- name: Make gradlew executable
script: |
chmod +x android/gradlew
- name: Build Android release (AAB and APK)
script: |
cd android
./gradlew bundleRelease assembleRelease
artifacts:
- android/app/build/outputs/**/*.aab
- android/app/build/outputs/apk/release/*.apk
5. Enable Proguard for APK Size Reduction (Optional)
Proguard is a tool to optimize and reduce the size of your APK. Enable it by editing android/app/build.gradle:
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = true
Important Note:
Proguard requires configuration specific to the native libraries used in your app. Edit the app/proguard-rules.pro file for proper configurations.
Summary
Follow the steps above to generate signed APK and AAB files, optionally optimize them with Proguard, and successfully release your React Native application.