8

Is there a way to use variables from build.gradle in my code, which is dependent on flavor AND buildType?

In this example here: Is it possible to declare a variable in Gradle usable in Java? the new resource value only depends on if its a debug or release build type.

What I would like to have is one variable for each possible buildVariant.

so something like:

flavor1Debug   => resValue "string", "app_name", "App 1 Debug"
flavor1Release => resValue "string", "app_name", "App 1"
flavor2Debug   => resValue "string", "app_name", "App 2 Debug"
flavor2Release => resValue "string", "app_name", "App 2"

Is there a nice way to do this either through build.gradle or another way that doesn't included switches or if else statements?

Community
  • 1
  • 1
just_user
  • 11,769
  • 19
  • 90
  • 135

1 Answers1

8

In build.gradle for your app, one way you could achieve this would be:

buildTypes {
    debug {
        productFlavors.flavor1.buildConfigField "String", "app_name", "\"App 1 Debug\""
        productFlavors.flavor2.buildConfigField "String", "app_name", "\"App 2 Debug\""
    }
    release {
        productFlavors.flavor1.buildConfigField "String", "app_name", "\"App 1\""
        productFlavors.flavor2.buildConfigField "String", "app_name", "\"App 2\""
    }
}

Note that you'll want to define your productFlavors{} block before your buildTypes.

gonga
  • 301
  • 3
  • 5
  • sorry I thought this worked first, but the app never get the value from debug, it always takes the release. No matter which build variant I select when I run the app. – just_user Apr 24 '15 at 15:23
  • Could you edit your post to include a more complete version of your build file? Also, which version of the gradle plugin are you using? I'm using 1.1.3 – gonga Apr 25 '15 at 01:01
  • Why the `"String"` declaration? – IgorGanapolsky Jul 13 '15 at 15:11