Adding Product Flavors to Android
If you need to develop more than one app for one project one might think of building two different android projects, fortunately, Android Studio gives us the liberty to create as many flavors as we want. Take for example Client
and Rider
Apps, the two are connected and share many classes, API among others. It would only make sense if we re-use these classes and have different directories for codes specific to each app. Take a look at the following screenshots.
You can see that the directories specific to either Rider
and Client
Apps have the respective names in the bracket and the ones which are shared arent. They are in main
directory and can be accessed by both.
So How Do We Make Such?
First, we need to add flavors
in the build.gradle
, note that if you have more than one module, you need to replicate the action for all build.gradle
files for the modules.
Everything in the default config can be added to flavors
Now if you sync the Gradle you will be able to see the flavors by navigating as follows.
Now that we have different flavors, what next?
It would be at least easy to differentiate what app uses what class, now every app is using the same classes and packages.
For the sake of the screenshots, I am going to another flavor called owner
as I had made what I wanted to screenshot using the other flavors.
You can see different directories for our different res
folders. Create for all the flavors.
Then navigate as shown below.
You can see the directories with all the flavors, note that main
is shared among all the flavors, but as of now, we can only add different resources, what if we want to have different Java classes?
Navigate as below.
Repeat for all the flavors, choosing Rider
Client
and Owner
In the last screenshot, you see Java is added inside the flavors. After that now we add our package to the Java folder, to make it seen on the Android Window later.
Make sure you do for all the flavors. Then navigate back to the Android window
Depending on the Flavor type selected you will see this
Now you can add flavor-specific code inside the package, if you change to another flavor, you will see the name changing and only classes for that flavor are shown. Note, that you can do the same with all android packages including tests, etc.
Now, let's add some drawables
and layouts
Choose the flavor you want to add that layout to, the same can be done to every other resource including strings
drawables
colors
etc.
What if you make mistake? it's easy as well,
Just check where you want to add it and select if it's not listed just change the main in the directory with the flavor name e.g Client or Rider.
Now your apps can run with different resources and classes, but now we want to change the different launcher activities, names, or app icons.
Navigate back to project view.
Expand main, remove the activities and just leave the application
We will add common things across the apps here, like permissions. Now copy the AndroidManifest file and paste it into all the flavors directory adding the Launcher activity as per the Launcher Activity of the apps.
Note, make sure you add tools:replace=”android:label” to all the manifest files for the different apps except the main. Now add different names, different icons, different launcher activities, etc.
And just like that, you have more than two apps from the same project.
Just change flavors by navigating to build variant on the left most panel of the Android studio when you want to change to different apps.
Get the code from https://github.com/ronnieotieno/Android-Product-Flavours
If you any question get me on Twitter @ronnieonly