NOTE
Complete demo configured with "multi-deployment testing" feature is here.
The Android Gradle plugin allows you to define custom config settings for each "build type" (like debug, release). This mechanism allows you to easily configure your debug builds to use your CodePush staging deployment key and your release builds to use your CodePush production deployment key.
NOTE: As a reminder, you can retrieve these keys by running code-push deployment ls <APP_NAME> -k
from your terminal.
To set this up, perform the following steps:
For React Native >= v0.60
-
Open the project's app level
build.gradle
file (for exampleandroid/app/build.gradle
in standard React Native projects) -
Find the
android { buildTypes {} }
section and defineresValue
entries for both yourdebug
andrelease
build types, which reference yourStaging
andProduction
deployment keys respectively.android { ... buildTypes { debug { ... // Note: CodePush updates should not be tested in Debug mode as they are overriden by the RN packager. However, because CodePush checks for updates in all modes, we must supply a key. resValue "string", "CodePushDeploymentKey", '""' ... } releaseStaging { ... resValue "string", "CodePushDeploymentKey", '"<INSERT_STAGING_KEY>"' // Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues // Add the following line if not already there matchingFallbacks = ['release'] ... } release { ... resValue "string", "CodePushDeploymentKey", '"<INSERT_PRODUCTION_KEY>"' ... } } ... }
NOTE: Remember to remove the key from
strings.xml
if you are configuring the deployment key in the build processNOTE: The naming convention for
releaseStaging
is significant due to this line.
For React Native v0.29 - v0.59
-
Open up your
MainApplication.java
file and make the following changes:@Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( ... new CodePush(BuildConfig.CODEPUSH_KEY, MainApplication.this, BuildConfig.DEBUG), // Add/change this line. ... ); }
-
Open your app's
build.gradle
file (for exampleandroid/app/build.gradle
in standard React Native projects) -
Find the
android { buildTypes {} }
section and definebuildConfigField
entries for both yourdebug
andrelease
build types, which reference yourStaging
andProduction
deployment keys respectively. If you prefer, you can define the key literals in yourgradle.properties
file, and then reference them here. Either way will work, and it's just a matter of personal preference.android { ... buildTypes { debug { ... // Note: CodePush updates should not be tested in Debug mode as they are overriden by the RN packager. However, because CodePush checks for updates in all modes, we must supply a key. buildConfigField "String", "CODEPUSH_KEY", '""' ... } releaseStaging { ... buildConfigField "String", "CODEPUSH_KEY", '"<INSERT_STAGING_KEY>"' // Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues // Add the following line if not already there matchingFallbacks = ['release'] ... } release { ... buildConfigField "String", "CODEPUSH_KEY", '"<INSERT_PRODUCTION_KEY>"' ... } } ... }
NOTE: The naming convention for
releaseStaging
is significant due to this line. -
Pass the deployment key to the
CodePush
constructor via the build config you defined, as opposed to a string literal.
For React Native v0.19 - v0.28
Open up your MainActivity.java
file and make the following changes:
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
...
new CodePush(BuildConfig.CODEPUSH_KEY, this, BuildConfig.DEBUG), // Add/change this line.
...
);
}
Note: If you gave your build setting a different name in your Gradle file, simply make sure to reflect that in your Java code.
And that's it! Now when you run or build your app, your debug builds will automatically be configured to sync with your Staging
deployment, and your release builds will be configured to sync with your Production
deployment.
NOTE: By default, the react-native run-android
command builds and deploys the debug version of your app, so if you want to test out a release/production build, simply run `react-native run-android --variant release. Refer to the React Native docs for details about how to configure and create release builds for your Android apps.
If you want to be able to install both debug and release builds simultaneously on the same device (highly recommended!), then you need to ensure that your debug build has a unique identity and icon from your release build. Otherwise, neither the OS nor you will be able to differentiate between the two. You can achieve this by performing the following steps:
- In your
build.gradle
file, specify theapplicationIdSuffix
field for your debug build type, which gives your debug build a unique identity for the OS (likecom.foo
vs.com.foo.debug
).
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
-
Create the
app/src/debug/res
directory structure in your app, which allows overriding resources (like strings, icons, layouts) for your debug builds -
Create a
values
directory underneath the debug res directory created in #2, and copy the existingstrings.xml
file from theapp/src/main/res/values
directory -
Open up the new debug
strings.xml
file and change the<string name="app_name">
element's value to something else (likefoo-debug
). This ensures that your debug build now has a distinct display name, so that you can differentiate it from your release build. -
Optionally, create "mirrored" directories in the
app/src/debug/res
directory for all of your app's icons that you want to change for your debug build. This part isn't technically critical, but it can make it easier to quickly spot your debug builds on a device if its icon is noticeable different.
And that's it! View here for more details on how resource merging works in Android.