Configuring “Product Flavors” in your Android app

👋 Hi, I'm Javal Nanda, a Mobile Application Developer based in India. I've been in the world of mobile app development since 2010, and this blog is my space to share experiences and thoughts.
Search for a command to run...

👋 Hi, I'm Javal Nanda, a Mobile Application Developer based in India. I've been in the world of mobile app development since 2010, and this blog is my space to share experiences and thoughts.
No comments yet. Be the first to comment.
We often need to modify the SwiftUI view in some way or the other. There are three ways in which we can achieve customization of the SwiftUI View: View Extensions ViewModifiers Custom Views We can almost produce the same results with the above a...

One of the main rules for writing clean code is that it should clearly show what it's meant to do. There are a few things to keep in mind when doing this. It means giving variables and function names that make sense, making sure each part of the code...

Pull requests are the standard way to request a change to be merged into the main or stable branch. Having fellow developers or maintainers review the change is crucial to ensure its quality, although exceptions may apply for teams using pair program...

Ideally, you would not require conditional logic in your production code to check if tests are running or the main app target is running. But, there are situations where you can't get around it in case of Integration tests or UI Tests. There are vari...

If you have used SPM to create the command line application before Swift 5.9.0, the project structure used to be as follows: Package.swift README.md .gitignore Sources/ Sources/SamplePackage/main.swift Tests/ Tests/LinuxMain.swift Tests/SamplePackage...

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:
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.