简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM