Search code examples
androidgradleandroid-gradle-pluginandroid-productflavors

Is it possible to declare a variable in gradle productFlavors and use it in Java?


I know that in buildTypes I can declare, but I want to declare in productFlavors.

like this:

productFlavors {
    f1 {
        some_variable: "v_f1"
    }
    f2 {
        some_variable: "v_f2"
    }
}

similar question :

Is it possible to declare a variable in Gradle usable in Java?


Solution

  • Yes, like this:

    productFlavors {
        f1 {
            buildConfigField "boolean", "aBoolean", "true"    
            buildConfigField "String", "aString", "foo"
        }
        f2 {
            buildConfigField "boolean", "aBoolean", "false"    
            buildConfigField "String", "aString", "bar"
        }
    }
    

    then at runtime you can access them like:

    if (BuildConfig.aBoolean) {
        // do something
    }