Simplified Your Android Workflow
Managing Environments Seamlessly with Build Variants in Android
Why do we use Flavours | Android build variants?
Android build variants are a useful tool for developers who want to create multiple versions of their app from a single codebase. This can be helpful for various reasons, such as:
Making separate app versions for different groups of users (e.g. free or paid)
Developing different app versions for different stages (e.g. testing or final release)
One of the most common use cases for Android build variants is to build different versions of an app for different environments. For example, you can create a development version of your app with debug symbols enabled, making it easier to debug. Alternatively, you can create a production version of your app with debug symbols disabled for security reasons.
Finally, Android build variants can be utilized to test different features or configurations of the app. For instance, you can create a build variant that incorporates a new feature that is still undergoing testing and then deploy that build variant to a small group of users to receive feedback.
Different Use Cases for Android Build Flavors: A Quick Summary
Free vs Paid
Different language builds
Development vs Production
Different screen sizes and resolutions
Different features and configurations
Enough Theory Let's start with a Practical Example (the given example is for the build.gradle.kts which is recommended by google)
Open the build.gradle file app level in your Android project.
In the
buildTypes
block, define two build types:release
anddebug
.For the
release
build type, set the following properties:debuggable
tofalse
. This prevents you from debugging your app in Android Studio.minifyEnabled
totrue
. This means that your app will be minified, which makes it smaller and faster.proguardFiles
to an array of ProGuard files. These files will be used to obfuscate your app and remove unused code.
For the
debug
build type, set the following properties:debuggable
totrue
. This allows you to debug your app in Android Studio.minifyEnabled
tofalse
. This means that your app will not be minified, which makes it easier to debug.
Product Flavors: Product flavors enable you to create different versions of your app for various use cases. For example, you might have flavors for "free" and "premium" versions of your app, each with unique features or branding. Each flavor can have its own resources, assets, and code.
I have created 4 different types of flavor ( for the different use cases )
dev
: This is the development build variant, with the application IDcom.ashutoshwahane.dev
and the version name suffix_dev
. It also defines a constant namedTEST_DEV_API
that stores the API key for the development environment.production
: This is the production build variant, with the application IDcom.ashutoshwahane.prod
and the version name suffix_prod
. It also defines a constant namedTEST_PROD_API
that stores the API key for the production environment.free
: This is the free build variant, with the application IDcom.ashutoshwahane.free
and the version name suffix_free
.paid
: This is the paid build variant, with the application IDcom.ashutoshwahane.paid
and the version name suffix_paid
.
On gradle.properties define your API key or any other config data. ( Bonus TIP: Add gradle.properties file to .gitIgnore to secure your API key )
Once all the steps are done make sure to Re-Build your project.
Now the keys are accessible anywhere in the app for ex:
The code val api = BuildConfig.API_KEY
retrieves the API key for the current build variant in Kotlin. The BuildConfig
class includes a static field named API_KEY
that stores the API key for the current build variant. The val
keyword declares a variable called api
, which is initialized to the value of the API_KEY
field.
Now open your Build Variants on Android Studio and you will see all the different flavors in release and debug variants.
No more changing the config manually just select the build variants and run the app
To learn more about build variants in Android Studio, please visit the official documentation: https://developer.android.com/studio/build/build-variants
If you have any questions about build variants, please feel free to leave a comment below. I hope this blog post was helpful. Thank you for reading!
Please subscribe to my blog for more Android development tips and tricks.