Monday, 20 March 2017

How to work with 'Product Flavors' in Android App?

I was asked to create multiple environments for the app which I've been working for a couple of months. My app needs to have two different package names, two different app/launcher icons and two sets of resources (i.e. strings, drawablescolors etc). The client asked me to point a flavor of the app to 'staging environment' and another to 'production environment' while keeping the source code of app same for both flavors. 

I googled for this requirement and implemented. 

productFlavors block defines a customized version of the application build by the project. A single project can have different flavors which change the generated application. This is the place where you need to configure the environments and all. 

I created 4 environments for my requirement. staging, production, internalTesting and stressTesting. You can create 'n' number of enviroments in the same way. 

Each environment needs to be identified uniquely. So I added different package names for each flavor of the app. 

'applicationIdSuffix' is used to add suffix for your actual applicationId/package name. My package name is 'co.whatever.client' So the package name for 'staging' app will become 'co.whatever.client.staging', and for 'internalTesting'co.whatever.client.internalhence you will be able to install and run all flavors of the app on the same device. If we don't change the applicationId, it won't allow you to run multiple apps with same package name on single device

'buildConfigField' is used to define flavor specific attributes. 
For example, If app needs to access a different base url for each flavor then 
you can configure it here.

I declared few business specific attributes of type string and boolean.

Now the question is: How can we access these variables in the code?
You can easily access them in the code like 
BuildConfig.BASE_URL, BuildConfig.IS_PRODUCTION


 

Run a specific flavor app:
To create the build for a specific flavor: Go to Build>Select Build Variant. 
It will display all the flavors that we are creating. 
Now select the flavor which you want to create the build for, and run your app.  

So now your app will use the base url of the flavor you selected and have
 package name as specified. You can change Build variant and choose another 
flavor of app and run. So now you can have two apps on your device with 
different package names each pointing to separate base url. 


Maintain different resources for each flavor:
What If you need to have different resources for each flavor?
We can even have different resources (i.e specific res folder for a flavor) for 
each flavor. Create the folders with the flavor names as shown below. 

You can maintain different folders like assets, res and json files like 
google-services (if any) inside the folder of respective flavor. 
So, now your app will have different launcher icons, strings and colors for each 
flavor for different clients. 

You can put flavor specific launcher icon inside: app\src\<flavor specific folder>
\drawable-xhdpi\ic_launcher.png
 
We don't need to have all the icons of main app in flavor specific folders. 
We should place icons which are differing from actual or main app. Same applies
to strings and colors. 

This is how we create multiple flavors of an app without changing even a single 
line of source code. 





No comments:

Post a Comment

How to work with 'Product Flavors' in Android App?

I was asked to create multiple environments for the app which I've been working for a couple of months. My app needs to have two differ...