Android

Product Flavours In Android Gradle Project

If you are looking for a solution to build different versions of your application with same source code then you have reached at the right place. This article will guide you on how to configure your app using Gradle script for applying different configurations on single codebase of your Android Project.

A flexible build system like Gradle and its Android specific plugin allows us to configure builds for different versions of the application.

Versions of app could be based on deployment environments (dev, qa, prod) or products (free, paid) or device (phone, tv, wear) on which app is running on.

“Product flavors represent different versions of your project that you expect to co-exist on a single device, the Google Play store, or repository.” – by Android Gradle DSL

Till Gradle introduced ProductFlavours we were only able to build application with the a single configuration or default configuration. Since the introduction of ProductFlavours, we are able to create variables based on build types.

Product Flavours, productFlavors { }, of Android Gradle Project enables us to apply different configurations on our single codebase.

Suppose we have 3 versions of app named, “dev” for development, “qa” for testing and “prod” for production. And for each version we need to use different app name, different base urls, build ids etc. Above requirement can be solved by the below build script,

android {
...
...

defaultConfig {
         applicationId "devdeeds.com.configurableapp"
         minSdkVersion 21
         targetSdkVersion 25
         versionCode 1
         versionName "1.0"
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     }     

productFlavors {

    dev {
        minSdkVersion '26'
        manifestPlaceholders = [appName:"DEV-DEVDEEDS"]
        buildConfigField "String", 'BASE_URL', "https://dev-api.devdeeds.com"
        applicationIdSuffix ".dev"
        versionNameSuffix "-dev"

    }

    qa {
        buildConfigField "String", 'BASE_URL', "https://qa-api.devdeeds.com"
        manifestPlaceholders = [appName:"QA-DEVDEEDS"]
        applicationIdSuffix ".qa"
        versionNameSuffix "-qa"
    }

    prod {
        buildConfigField "String", 'BASE_URL', "https://dev-api.devdeeds.com"
        manifestPlaceholders = [appName:"PROD-DEVDEEDS"]
        applicationIdSuffix ".prod"
        versionNameSuffix "-prod"
    }


}
...
...
}

Once you done with ProductFlovours, you have to do gradle sync your project. The result will be seen in the “Build Variants” window of Android Studio IDE. It is important to select appropriate build variant in this window before you start development.

What happens under the hood is, the defaultConfig block of the gradle script will be overridden by the corresponding ProductFlover’s block.

During build time, Gradle generates the BuildConfig.java class in which you can check and confirm for custom fields like, BASE_URL created using the buildConfigField()

Extracting the configurable items from the code base to build scripts is one of the important process in defining ProductFlovours in build.gradle. If you are able to do this then you can create different versions of your app in Google Playstore, Version repositories or on the same device.

All the supported properties can be found here http://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.ProductFlavor.html

About author

Geethu is a teacher by profession, but she likes to research new things and is also interested in writing. Devdeeds is her blog, where she writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *