Android using Gradle Build flavors in the code like an if case

BuildConfig.FLAVOR gives you combined product flavor.
So if you have only one flavor dimension:

productFlavors {
    normal {
    }

    admin {
    }
}

Then you can just check it:

if (BuildConfig.FLAVOR.equals("admin")) {
    ...
}

But if you have multiple flavor dimensions:

flavorDimensions "access", "color"

productFlavors {
    normal {
        dimension "access"
    }

    admin {
        dimension "access"
    }

    red {
        dimension "color"
    }

    blue {
        dimension "color"
    }
}

there are also BuildConfig.FLAVOR_access and BuildConfig.FLAVOR_color fields so you should check it like this:

if (BuildConfig.FLAVOR_access.equals("admin")) {
    ...
}

And BuildConfig.FLAVOR contains full flavor name. For example, adminBlue.

Leave a Comment