Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

build gradle variables to be used in code depending on flavor AND build type

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?

like image 618
just_user Avatar asked Apr 07 '15 09:04

just_user


1 Answers

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.

like image 108
gonga Avatar answered Sep 19 '22 01:09

gonga