Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

android - Gradle - Add conditional classpath in buildscript dependencies

I have updated to Android Studio 2.2, which uses by default the Gradle Plugin v2.2.0, and is much better for debugging purposes. For disribution purposes, I must still use v2.1.3. I was thinking of adding a conditional command in the project gradle script, but I am not sure how to do it. The following test works

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 cannot figure out how to do it. Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, I believe I solved it, and it is very simple. You need to check the gradle.startParameter.taskNames property. Here is how I coded 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'
    }
}

So far it is working fine. If you prefer, you can change the "Release" value, to a flavor variant (if you are using flavors).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...