1

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?

prom85
  • 14,805
  • 15
  • 98
  • 206

2 Answers2

3

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
    }
}
Bipi
  • 2,924
  • 1
  • 16
  • 20
  • `new Setup()` => this line does not work. I did not need it yet, now I want to define a user variable as well with this method. any ideas what I'm missing? – prom85 Oct 21 '16 at 07:15
  • I wrote "new Setup()", but this is your own class. I don't know how you've defined it. – Bipi Oct 21 '16 at 07:41
  • I think this is not necessary at all. Seems like following works: `if (!project.hasProperty('versions.supportLib')) { versions.supportLib = "25.0.0"; }` No need for "creating" the setup before... – prom85 Oct 21 '16 at 07:42
  • You might have a look to this : http://mrhaki.blogspot.fr/2016/02/gradle-goodness-create-objects-with-dsl.html and this https://docs.gradle.org/current/userguide/custom_plugins.html. It may not be related to your problem but it might be interesting. – Bipi Oct 21 '16 at 07:49
1

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"
        ...
    }
}
Joey Dalu
  • 1,073
  • 7
  • 13