Reputation: 17788
In an open source library of mine, I use something like following:
android {
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
I don't want to force everyone to define those constants, but I want to use them and I use the same constants in all my projects and libraries. So I want to be able to use one code that works for me and for everyone else not defining those variables. I'm looking for something like following:
android {
// pseudo code line
if setup is defined
{
compileSdkVersion setup.compileSdk
buildToolsVersion setup.buildTools
defaultConfig {
minSdkVersion setup.minSdk
targetSdkVersion setup.targetSdk
}
}
// default setup, if the user did not define global constants
else
{
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
}
}
}
Is something like that possible? Any alternative suggestions?
Upvotes: 1
Views: 5829
Reputation: 3579
I imagine that your setup variable is stored in project extension
project.ext.setup = setup
This way, it can be accessible from your project and all your subprojects
You can test the existence of setup like this
if (project.hasProperty('setup'))
The idea is to create a default setup var if no one is provided
if (!project.hasProperty('setup')){
project.ext.setup = new Setup()
project.setup.compileSdk = 24
project.setup.buildTools = "24.0.2"
project.setup.minSdk = 16
project.setup.targetSdk = 24
}
android {
compileSdkVersion project.setup.compileSdk
buildToolsVersion project.setup.buildTools
defaultConfig {
minSdkVersion project.setup.minSdk
targetSdkVersion project.setup.targetSdk
}
}
Upvotes: 3
Reputation: 1113
Sure, I've tried this and it built successfully.
android{
...
if (someCondition){
compileSdkVersion 23
buildToolsVersion "23.0.1"
...
}else {
compileSdkVersion 24
buildToolsVersion "24.0.3"
...
}
}
Upvotes: 1