Use Android Build Variants To Manage Server Environments
Build, run, and deploy multiple apps simultaneously
Takeaway From the Article
By the end of this article, you’ll have learned how to use build variants to manage your server’s development, staging, and production environments. With this approach, you no longer need to update anything before generating APKs.
Note: This is an experimental approach; if you’ve any suggestions for enhancements or you find bugs, let me know in the comments.
Introduction
Android, by default, uses two build types: debug and release. When you run an app through the Android studio, it will install debug APK, and while building signed APK, you can see both build types.
If you’ve implemented any product flavors, you’ll see these two variants for each flavor. Build types are different from product flavors. Product flavors are the top-level distribution of app variants, and build types are internal variants of each flavor.
A common use case to demonstrate product flavors and build variants is free and paid versions. Here free and paid are product flavors, whereas release and debug are build variants. Finally, you’ll end up with four build variants, as shown below:
Product flavors are a powerful feature in the Android Gradle system that allows you to create different app variants with the same code base. It gives the flexibility to maintain whatever is required in all variants on the main module and create a separate module for each flavor to implement independent code. To learn more about it, read the following article:
Don’t confuse product flavors with build variants; product flavors are the top-level distribution of app variants, and build types are internal variants of each flavor.
Now that you know the difference between product flavors and build variants, it’s time to use build variants to manage server environments. Usually, the development process contains three environments: development, staging, and production.
Development: Usually, developers build and test the new implementation in this environment.
Staging: Once the developers are finished with implementation and done with their testing, they’ll move to staging, where the main testing and final approval to move production take place.
Production: If the application is approved, it’ll go to the production server, where it’s available to end users.
Server Environments With Build Variants
Now that we know what build variants and different server environments are, it’s time to make use of build variants to generate different server environment builds.
Typically, developers maintain three different sets of details for each variant. Only one set is active at any given time; we’ve to update it manually based on the type of build that is being generated. This is the process that I’ve followed over the years, but I don’t think it’s the best approach because it involves manual work on each build generation. It has the scope to be automated.
My idea to automate this process is to maintain three different build variants serving each server environment. We need to declare build variants buildTypes
inside the android
tag. This results in five build variants: release
, debug
, prod
, stg
, and dev
. Have a look: