Default value for Gradle buildConfigField boolean used across flavors

AndroidGradleAndroid Gradle-Plugin

Android Problem Overview


I have a number of flavors in my app, and I want to set a boolean buildConfigField for a subset of them. Is there a way to avoid having to add the field to every flavor? Ideally my build.gradle would look like the following:

productFlavors {
    flavor1 {
    }

    ....

    flavor4 {
        buildConfigField "boolean", "DISABLE_SOMETHING", "true"
    }

    flavor5 {
        buildConfigField "boolean", "DISABLE_SOMETHING", "true"
    }

    ....

    flavor8 {
    }
}

So in my app I can just go

if (BuildConfig.DISABLE_SOMETHING) {
    //disable stuff
}

However, compilation fails when I try to build with, for example, flavor1, as it can't find the field. I don't want to have to remember to add this to every new flavor I create. Are there any ways around this?

Android Solutions


Solution 1 - Android

You can use defaultConfig for this (inside android{})

defaultConfig {
  buildConfigField "boolean", "DISABLE_SOMETHING", "true"
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - AndroidaxayView Answer on Stackoverflow