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.
buildTypesblock, define two build types:
releasebuild type, set the following properties:
false. This prevents you from debugging your app in Android Studio.
true. This means that your app will be minified, which makes it smaller and faster.
proguardFilesto an array of ProGuard files. These files will be used to obfuscate your app and remove unused code.
debugbuild type, set the following properties:
true. This allows you to debug your app in Android Studio.
false. 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 ID
com.ashutoshwahane.devand the version name suffix
_dev. It also defines a constant named
TEST_DEV_APIthat stores the API key for the development environment.
production: This is the production build variant, with the application ID
com.ashutoshwahane.prodand the version name suffix
_prod. It also defines a constant named
TEST_PROD_APIthat stores the API key for the production environment.
free: This is the free build variant, with the application ID
com.ashutoshwahane.freeand the version name suffix
paid: This is the paid build variant, with the application ID
com.ashutoshwahane.paidand the version name suffix
Once all the steps are done make sure to Re-Build your project.
Now the keys are accessible anywhere in the app for ex:
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
valkeyword declares a variable called
api, which is initialized to the value of the
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.
Did you find this article valuable?
Support Ashutosh Wahane by becoming a sponsor. Any amount is appreciated!