Android Product Flavors
Recently, I experienced product flavors in Android development and it is fantastics feature. I want to give you some keywords and what they means basically.
Build Type
When you create a new module, Android Studio automatically creates the debug and release build types for you. Actually we use it every day unaware. When you develop your application it compiles in debug mode by default(If you don’t change build variant. We will come this later.).
So what is build type for? It decides that how our code will be compiled. For instance, If we want to sign our .apk with debug key, we put our debug configuration into debug build type. If we want to have obfuscated code when it is compiled and ready to release, we put that configuration on our release build type. If we want to log our HTTP request in debug mode but we want to disable it on release mode, we put that configurations on build types or call build types in library dependencies.
Every build type has same codebase and same UI behaviour with different compile configurations.
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
}
I think this is the simplest example of build types. If you want to create different application on release and debug build. You have to change your applicationId.
defaultConfig {
applicationId "com.sampleproject"
...
}
when you run your app in debug mode, your application package name will be com.sampleproject.debug and if you run your app in release mode, your application package name will be com.sampleproject.
If you want to add more property to build types like signingOptions, debuggable, useJack, versionNameSuffix and more, you should check this link.
Product Flavor
Documentations and blogposts are saying that product flavor is like build type. First question is that comes to my mind is that If there are like each other, why don’t we use just build types instead of product flavors? Keep this question on the edge of your mind. We will come this later.
Everyone is giving same example “free” “paid” version of your app. But I will give another one. Let’s say you are developing your app for your customer users. Everything is going fine for customer app. Then your product owner said that you need to develop that app for admin users. Admin user app should have all functionalities that customer app has. But also admin user can have access to statistics page and admin user should see the app in different colours and resources. And also your admin app’s analytics should not be mixed with customer app.What will you do? The answer is Product Flavor. Same app, different behaviour.
Edit your gradle file.
android {
...
defaultConfig {...}
buildTypes {...}
productFlavors {
admin {
..
}
customer {
..
}
}
}
And add your flavor folders under /src/
Build Variant
Build variant combines your build types and product flavors. Sync your project after you update your build.gradle. Then you will see all your build variants.
If you want to compile your project for admins then choose adminRelease. If you want to debug your customer app then you should choose customerDebug.
Remember that question? buildTypes vs productFlavors.
I found a great doc about Gradle DSL. Methods, properties, script blocks are different for build types and product flavor. You should absolutely check them out.
Product Flavor properties, methods, scripts
Build Type properties, methods, scripts
Thanks for reading.I am so new on product flavors. If you have any recommendation or idea about product flavor or build type, please don’t hesitate to comment:)