Product flavors

As opposed to build types, which are used to configure several different builds of the same app or library, product flavors are used to create different versions of the same app. The typical example is an app that has a free and a paid version. Another common scenario is an agency that builds apps that have the same functionality for several clients, where only the branding changes. This is very common in the taxi industry or with banking apps, where one company creates an app that can be reused for all clients in the same category. The only things that change are the main colors, the logo, and the URL of the backend. Product flavors greatly simplify the process of having different versions of an app based on the same code.

If you are unsure whether you need a new build type, or a new product flavor, you should ask yourself if you want to create a new build of the same app for internal use, or a new APK to publish to Google Play. If you need an entirely new app that needs to be published separately from what you already have, then product flavors are the way to go. Otherwise, you should stick to using build types.

Creating product flavors

Creating product flavors is very similar to creating build types. You can create a new product flavor by adding it to the productFlavor block, like this:

android {
    productFlavors {
        red {
            applicationId 'com.gradleforandroid.red'
            versionCode 3
        }
        blue {
            applicationId 'com.gradleforandroid.blue'
            minSdkVersion 14
            versionCode 4
        }
    }
}

Product flavors have different properties than build types. That is because product flavors are objects of the ProductFlavor class, just like the defaultConfig object that is present in all build scripts. This means that defaultConfig and all your product flavors share the same properties.

Source sets

Just like with build types, product flavors can have their own source set directories. Creating a folder for a specific flavor is as easy as creating a folder with the flavor name. You can even go one step further and create a folder specifically for a combination of a certain build type and flavor. The name of the folder would then be the flavor name followed by the build type name. For example, if you want to have a different app icon specifically for the release version of the blue flavor, the folder would have to be called blueRelease. The components of the combined folder will have a higher priority than the components from both the build type folder and the product flavor folder.

Multiflavor variants

In some cases, you might want to take it further and create combinations of product flavors. For example, client A and client B might each want free and paid versions of their app, which is based on the same code base, but has different branding. Creating four different flavors would mean having several duplicate settings, so that is not the way to go. Combining flavors in an efficient way is possible using flavor dimensions, like this:

android {
    flavorDimensions "color", "price"

    productFlavors {
        red {
            flavorDimension "color"
        }

        blue {
            flavorDimension "color"
        }
        free {
            flavorDimension "price"
        }

        paid {
            flavorDimension "price"
        }
    }
}

As soon as you add the flavor dimensions, Gradle expects you to specify a dimension for each flavor. If you forget, you will get a build error with a message explaining the issue. The flavorDimensions array defines the dimensions, and the order of the dimensions is very important. When combining two flavors, they might have defined the same properties or resources. In that case, the order of the flavor dimensions array determines which flavor configuration overrides the other. In the earlier example, the color dimension overrides the price dimension. The order also determines the name of the build variant.

Assuming the default build configuration with the debug and release build types, defining the flavors as shown in the previous example will generate all of these build variants:

  • blueFreeDebug and blueFreeRelease
  • bluePaidDebug and bluePaidRelease
  • redFreeDebug and redFreeRelease
  • redPaidDebug and redPaidRelease
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.208