After the initial release of our android app, we wanted a staging environment that uses different app config and secrets. Manually changing and replacing the files are time consuming and an annoying process. So in this post, I will cover how we setup our staging version for our android app.

STEP 1: Adding staging build variant in android/app/build.gradle

buildTypes{

//... add below lines inside buildTypes

releasestaging {

initWith release

applicationIdSuffix ".releasestaging"

matchingFallbacks = ['release']

}

// ...

}

let’s have a quick look on whats going inside this snippet. The initWith property allows you to copy configurations from other build types. applicationIdSuffix property allows to add a suffix to default applicationId this is required to differentiate the package name generated from release and staging variants. matchingFallbacks is used to specify alternative matches for a given build type. matchingFallbacks is required only if you have any native packages such as react-native-firebase, react-native-device-info etc.

note: while naming the build variant make sure it starts with release or you have to edit node_modules/react-native/react.gradle . You can refer this link on how to support a name without the release prefix.

STEP 2: configure hermes path in android/app/build.gradle

if (enableHermes) {

def hermesPath = "../../node_modules/hermesvm/android/";

debugImplementation files(hermesPath +"hermes-debug.aar")

releaseImplementation files(hermesPath +"hermes-release.aar")

// add this line to configure hermes for staging

releasestagingImplementation files(hermesPath +"hermes-release.aar") } else {

implementation jscFlavor

}

STEP 3: (optional) if you are using react-native-firebase

To have different google-firebase.json files for each build type, we have to organise the project structure like this:

android/

app/

google-services.json

src/debug/google-services.json

src/releasestaging/google-services.json

src/development/google-services.json

also change the package name in android/app/src/releasestaging/google-services.json to com.yourappname.releasestaging (same as the build type’s applicationSuffixId given in /android/app/build.gradle )

STEP 4: Changing the app name and icon to differentiate build types

Now let’s change name and icon to YourApp Staging

To change the app icon , we must put ic_launcher.png inside staging folder like this

android/app/src/releasestaging

├── google-services.json

└── res

├── mipmap-hdpi

│ ├── ic_launcher.png

│ └── ic_launcher_round.png

├── mipmap-mdpi

│ ├── ic_launcher.png

│ └── ic_launcher_round.png

├── mipmap-xhdpi

│ ├── ic_launcher.png

│ └── ic_launcher_round.png

├── mipmap-xxhdpi

│ ├── ic_launcher.png

│ └── ic_launcher_round.png

├── mipmap-xxxhdpi

│ ├── ic_launcher.png

│ └── ic_launcher_round.png

└── values

└── strings.xml

Change the app name in the releasestaging/res/values/strings.xml