Gradle - Add conditional classpath in buildscript dependencies

I updated the version for Android Studio 2.2, which uses Gradle Plugin v2.2.0 by default and is much better for debugging. For breeding purposes, I should still use v2.1.3. I was thinking of adding a conditional command to the Gradle script project, but I'm not sure how to do this. Next test work

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        if (project.name.startsWith("X"))
        {
            classpath 'com.android.tools.build:gradle:2.1.3'
        }
        else
        {
            classpath 'com.android.tools.build:gradle:2.2.0'
        }
    }
}

But I need it to be something like

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        if (IS_RELEASE_VERSION)
        {
            classpath 'com.android.tools.build:gradle:2.1.3'
        }
        else
        {
            classpath 'com.android.tools.build:gradle:2.2.0'
        }
    }
}

and I can’t figure out how to do this. thanks in advance

+4
source share
1 answer

Well, I think I have decided, and it is very simple. You need to check the property gradle.startParameter.taskNames. This is how I encoded it:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        if (gradle.startParameter.taskNames.size() > 0 && gradle.startParameter.taskNames.get(0).contains("Release"))
        {
            classpath 'com.android.tools.build:gradle:2.1.3'
        }
        else
        {
            classpath 'com.android.tools.build:gradle:2.2.0'
        }
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

. , "Release" ( ).

+4

Source: https://habr.com/ru/post/1655248/


All Articles