Simplified Your Android Workflow

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)

  1. Open the build.gradle file app level in your Android project.

  2. In the buildTypes block, define two build types:release and debug.

  3. For the release build type, set the following properties:

    • debuggable to false. This prevents you from debugging your app in Android Studio.

    • minifyEnabled to true. 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.

  4. For the debug build type, set the following properties:

    1. debuggable to true. This allows you to debug your app in Android Studio.

    2. minifyEnabled to false. This means that your app will not be minified, which makes it easier to debug.

  5. 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 )

    1. dev: This is the development build variant, with the application ID com.ashutoshwahane.dev and the version name suffix _dev. It also defines a constant named TEST_DEV_API that stores the API key for the development environment.

    2. production: This is the production build variant, with the application ID com.ashutoshwahane.prod and the version name suffix _prod. It also defines a constant named TEST_PROD_API that stores the API key for the production environment.

    3. free: This is the free build variant, with the application ID com.ashutoshwahane.free and the version name suffix _free.

    4. paid: This is the paid build variant, with the application ID com.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 valkeyword declares a variable called api, which is initialized to the value of the API_KEY field.

  1. 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!