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
Node.js and npm/yarn installed.
React Native CLI installed.
Android Studio installed and set up.
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
andmyapp
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
In your project root, create a new file
android/
keystore.properties
(don’t commit this file to version control).Add the following content:
storeFile=../android/app/myapp.keystore
storePassword=YourKeystorePassword
keyAlias=myapp
keyPassword=YourKeyPassword
- Add
keystore.properties
to your.gitignore
:
android/keystore.properties
Step 3: Update build.gradle Securely
- 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))
}
- 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
Transfer the APK to your device or emulator for testing.
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
Create a Google Play Developer account.
Upload the AAB file and complete the app listing.
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.