Android product flavors are variants of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.
Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customized versions of products. They form part of what we call Build Variants.
Build Variants Build variants are the result of Gradle using a specific set of rules to combine settings, code, and resources configured in your build types and product flavors.
Build Type applies different build and packaging settings. An example of build types are “Debug” and “Release”.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
buildTypes { debug { debuggable true minifyEnabled false shrinkResources false } release { debuggable false minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } |
When to use Product Flavors
- When we want to address the issue of having separate project code for each version of the app while still having one project code.
- Given a scenario where you have a free and a paid app you can limit features in the free and expose all the other features in the paid version of the app.
- Given another scenario where you want to implement region-specific functions depending on the country, you can use product flavors for such a use case.
- White labeling (these are apps that are developed by a certain company, and they are re-branded and resold by other companies).
- Given a scenario where the Admin user app should have all functionalities that the customer app has. But also admin users can have access to the statistics page and admin users should see the app in different colors and resources. And also your admin app’s analytics should not be mixed with a customer app.
Pros and Cons
Pros
- They address the issue of having a separate project code base for each version of the app.
- They make the code much easier and faster to navigate through the code base as everything related to the specific product flavor would be kept in their corresponding folders.
Cons
- The more variants, the greater the complexity which thereby makes it harder to maintain the codebase.
- IDEs sometimes takes time to build the project after switching between variants.
Product Flavors Creation
For creating product flavors in android, we just need to add productFlavors block inside the android block in app-level build.gradle file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
android { ... defaultConfig {...} buildTypes {...} productFlavors { admin { .. applicationId "abc.admin.android" } customer { .. applicationId "abc.customer.android" } } } |
You can now create source sets folders app/src/admin/ and app/src/customer/ parallel to app/src/main/. The common classes and resources can stay in app/src/main while the flavor dependent code can go in the respective folders.
Resource Files Creation
Switch to project view in the left panel and then expand all directories until src and then right-click on src then navigate to
new > Folder > Res Folder
Then you’ll see a popup with two fields
Target Source set is nothing but the source code directories for selected flavor.
The New folder location is the location where the file will be created.
Similarly, we can create a directory for all flavored build types if necessary.
Java Files Creation
Creating a java directory for each flavor is similar to resource directories creation. The only difference is that we select a java folder instead of the Res folder.
Build Variant
Build variant combines your build types and product flavors. Sync your project after you update your build.gradle, select Build > Select Build Variant in the menu bar, and you will see the different Build Variants auto-generated when you added the Product flavors.