-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,165
  • 14
  • 22
aotian16
  • 767
  • 1
  • 10
  • 21

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
  • 3,063
  • 1
  • 15
  • 17
0

Sure .

Access it via BuildConfig.some_variable.

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

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59