Updated 29 July 2020
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' } } |
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.
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.
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 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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.