-1

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?

Artyom
  • 1,089
  • 14
  • 20
aotian16
  • 591
  • 1
  • 6
  • 19

2 Answers2

4

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
}
Alessio
  • 2,696
  • 13
  • 16
0

Sure .

Access it via BuildConfig.some_variable.

You will get the variable value according to the build variant .

Don Chakkappan
  • 6,872
  • 5
  • 41
  • 54