Configuring “Product Flavors” in your Android app

Configuring “Product Flavors” in your Android app

Product Flavors: Image source: https://developer.android.com/

Product Flavors is a great way to generate multiple APK with different configurations or flavors(demo/full/staging/production/uat) of your application.

Listing down few scenarios where Product Flavors can be useful:

  1. Generate APK with different package name

The most common scenario where Product Flavors can be useful is to maintain different package name for your Staging, UAT and Production builds so that you can install those builds on a single device without overriding different environment builds.

There are two ways to do it either you can set applicationId or you can set applicationIdSuffix. The ideal way would be to use applicationIdSuffix and define applicationId in the defaultConfig block in your build.gradle file

Refer to image below where I have defined two flavors (staging and production) for my application and have applied applicationIdSuffix for staging configuration. I have not set applicationIdSuffix for production configuration as I don’t want my live app on store to have package name ending with .production

As soon as you add productFlavors in your app’s build.gradle file. You will be able to select Build Variants related to productFlavor you defined as observed in image below.

2. Defining different HOST/API URL

You can define different HOST url using productFlavors when you have separate url pointing to staging/dev/uat/product environments.

You can then access this baseUrl in your ServiceGenerator class using BuildConfig.HOST.

3. Defining manifestPlaceholders

Refer to below image where I have applied different values for onesignal_app_id and onesignal_google_project_number for staging and production environment.

4. Applying different app name.

In point 1. we learned how to apply different package name to install different variants of app in same device but we didn’t change the app name which will confuse the user in determining which app is production version and which app is dev/staging version.

We can apply different name based on flavors as below:

These are some of the most common use cases where productFlavors can be useful. Product Flavors is very powerful and you can customize much more than these examples.

You can define all the properties used under defaultConfig for configuration of productFlavors as defaultConfig belongs to ProductFlavor class.

Happy coding fellow developers. If you find this article helpful feel free to spread the info by hitting ♥ below.

Did you find this article valuable?

Support Javal Nanda by becoming a sponsor. Any amount is appreciated!