Gradle 插件区分 AGP 版本

Gradle 插件区分 AGP 版本

01 January 2018

有时候,在写 android groovy 插件的时候,需要对 AGP 版本进行判断。从而,适配插件在不同版本的 AGP 上的运行效果。




方法一


这是从某 android groovy 插件看到的源码,我也忘了是什么插件。总之,简单粗暴。


static final String VERSION_3_ZERO_FIELD = "com.android.builder.Version"  
// <= 3.0
static final String VERSION_3_ONE_FIELD = "com.android.builder.model.Version"  
// > 3.1
static final String AGP_VERSION_FIELD = "ANDROID_GRADLE_PLUGIN_VERSION"

String gradlePluginVersion = ''  
try {  
    gradlePluginVersion = Class.forName(VERSION_3_ZERO_FIELD).
            getDeclaredField(AGP_VERSION_FIELD).
            get(this).
            toString()
} catch (Exception e) {
    // ingnore
}
try {  
    gradlePluginVersion = Class.forName(VERSION_3_ONE_FIELD).
            getDeclaredField(AGP_VERSION_FIELD).
            get(this).
            toString()
} catch (Exception e) {
    // ingnore
}




方法二


这是我自己写的方法,还行。我一般作为 方法一 拿不到后的备选方法。也就是 二级策略


String alpha = ''  
String version = ''  
project.rootProject  
        .buildscript
        .configurations
        .classpath
        .resolvedConfiguration
        .firstLevelModuleDependencies.
        each {
            def name = it.name
            if (name.contains('com.android.tools.build:gradle')) {
                def moduleVersion = it.moduleVersion
                // alpha ?
                if (moduleVersion.contains("-")) {
                    def versionArray = moduleVersion.split("-")
                    version = versionArray[0]
                    alpha = versionArray[1]
                } else {
                    version = moduleVersion
                }
            }
        }
def firstVersion = version.substring(0, 1) as Integer  
if (version == '3.1.0') {  
    if ('' != alpha) {
        if (alpha == "alpha01") {
            // alpha01
        } else {
            // alpha02, alpha03...
        }
    } else {
        // 3.1.0 final version
    }
} else if (version == '3.0.0' || firstVersion < 3) {
    // less than or equal to 3.0.0
}




综合策略


结合 方法一方法二。得出的,稍微较好的策略。


def dispatchAndroidGradlePluginVersion(Project project,  
        Closure lessThanThreeClosure,
        Closure finalClousure,
        Closure... closures) {
    final String VERSION_3_ZERO_FIELD = "com.android.builder.Version"
    // <= 3.0
    final String VERSION_3_ONE_FIELD = "com.android.builder.model.Version"
    // > 3.1
    final String AGP_VERSION_FIELD = "ANDROID_GRADLE_PLUGIN_VERSION"

    String gradlePluginVersion = ""
    try {
        gradlePluginVersion = Class.forName(VERSION_3_ZERO_FIELD).
                getDeclaredField(AGP_VERSION_FIELD).
                get(this).
                toString()
    } catch (Exception e) {
        // ignore
    }
    try {
        gradlePluginVersion = Class.forName(VERSION_3_ONE_FIELD).
                getDeclaredField(AGP_VERSION_FIELD).
                get(this).
                toString()
    } catch (Exception e) {
        // ignore
    }

    String alpha = ''
    String version = ''
    if (gradlePluginVersion != "") {
        if (gradlePluginVersion.contains("-")) {
            def versionArray = gradlePluginVersion.split("-")
            version = versionArray[0]
            alpha = versionArray[1]
        } else {
            version = gradlePluginVersion
        }
    } else {
        project.rootProject
                .buildscript
                .configurations
                .classpath
                .resolvedConfiguration
                .firstLevelModuleDependencies.
                each {
                    def name = it.name
                    if (name.contains('com.android.tools.build:gradle')) {
                        def moduleVersion = it.moduleVersion
                        // alpha ?
                        if (moduleVersion.contains("-")) {
                            def versionArray = moduleVersion.split("-")
                            version = versionArray[0]
                            alpha = versionArray[1]
                        } else {
                            version = moduleVersion
                        }
                    }
                }
    }
    def firstVersion = version.substring(0, 1) as Integer
    if (version == '3.1.0') {
        if ('' != alpha) {
            if (alpha == "alpha01") {
                // alpha01
                closures[1].call()
            } else {
                // alpha02, alpha03...
                // closures[2].call(),closures[3].call()
            }
        } else {
            // 3.1.0 final version
            finalClousure.call()
        }
    } else if (version == '3.0.0' || firstVersion < 3) {
        // less than or equal to 3.0.0
        lessThanThreeClosure.call()
    }
}