Reputation: 1379
I want to set different values for a property depending on which buildType was chosen. But I realized by checking the gradle console that in configuration phase all 3 buildTypes are executed and the value of the last build remains in the property. I think that's not how it should work. Did I do something wrong?
android {
signingConfigs {
...
}
compileSdkVersion 28
defaultConfig {
...
}
productFlavors {
normal {
manifestPlaceholders = [appName: "@string/app_name"]
signingConfig signingConfigs.configNormalRelease
}
}
buildTypes {
release {
println("app release build")
rootProject.ext.test = false
}
debug {
println("app debug build")
rootProject.ext.test = false
}
staging {
println("app staging build")
rootProject.ext.test = true
}
}
sourceSets {
main {
aidl.srcDirs = ['src/main/aidl']
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {
checkReleaseBuilds true
abortOnError false
}
flavorDimensions 'tier'
productFlavors {
normal {
dimension "tier"
}
}
}
Output is
app release build
app debug build
app staging build
value of 'test' property is always true, whether I call assembleRelease, assembleDebug, assembleStaging
Why I see everywhere that people put api keys, urls, and other custom stuff into buildTypes when they get overridden by the last one? I understand that gradle might call all buildtypes during configuration phase, but where to put above mentions values then?
Upvotes: 1
Views: 469
Reputation: 1380
If you want to get your test
value from source code in your project, you can set variable:
buildTypes {
release {
buildConfigField 'boolean', 'test', 'false'
}
debug {
buildConfigField 'boolean', 'test', 'false'
}
staging {
buildConfigField 'boolean', 'test', 'true'
}
}
And then you can get this field from your code using BuildConfig
class (test
field will be generated intoBuildConfig
). Get it through BuildConfig.test
.
And if you really want to change gradle variable for any buildTypes, you can try to do this:
android.applicationVariants.all { variant ->
boolean isStaging = gradle.startParameter.taskNames.find {it.contains("staging") || it.contains("Staging")} != null
if (isStaging) {
rootProject.ext.test = true
} else {
rootProject.ext.test = false
}
}
Upvotes: 3