Based on this answer, I realized that we can use Gradle variables (I'm not familiar with Gradle of course, so excuse my terminology) to make some consistencies across many Android projects.
For example, I want to change the android
closure configuration from this:
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
}
To this:
android {
compileSdkVersion configurationVariables.sdk
buildToolsVersion configurationVariables.buildToolsVersion
defaultConfig {
minSdkVersion configurationVariables.minSdk
targetSdkVersion configurationVariables.targetSdk
versionCode 1
versionName "1.0"
}
}
However, I get this error:
Error:(5, 0) startup failed: build file 'path_to_android\build.gradle': 5: Statement labels may not be used in build scripts. In case you tried to configure a property named 'buildToolsVersion', replace ':' with '=' or ' ', otherwise it will not have the desired effect. @ line 5, column 24. buildToolsVersion: configurationVariables.buildToolsVersion
How can I use variables to centralize my build configuration across projects and modules?
Update: I'm defining my configurationVariables
as follow:
ext {
configurationVariables = [
sdk = 27,
buildToolsVersion = "27.0.3",
minSdk = 16,
targetSdk = 27
]
}
I write this in a config.gradle
file and use apply from
to import it in the build.gradle
of the root project to apply it on all subprojects.
your config file structure store value as a varible. Generally this structure is use to store variable.Your config file should be like this
ext {
sdk = 27
buildToolsVersion = "27.0.3"
minSdk = 16
targetSdk = 27
}
and you use this variable as
compileSdkVersion sdk
buildToolsVersion buildToolsVersion
I haven't use array for storing this variable but as you given in another answer link they store array variable with colon(:) and you are directly storing values. I am not sure but try to use colon like this if you want to use an array :
ext {
configurationVariables = [
sdk : 27,
buildToolsVersion : "27.0.0",
minSdk : 16,
targetSdk : 27
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With