插件内获取 Android gradle plugin 版本号

在这里插入图片描述

private fun checkAgpVersion(project: Project) {
      project.plugins.withId("com.android.base") {
          val versionValue = try {
              val versionClass = try {
                  it::class.java.classLoader.loadClass("com.android.Version")
              } catch (exception: ClassNotFoundException) {
                  it::class.java.classLoader.loadClass("com.android.builder.model.Version")
              }
              val field =
                  versionClass.fields.find { it.name == "ANDROID_GRADLE_PLUGIN_VERSION" }!!
              field.get(null) as String
          } catch (ex: Throwable) {
              PluginLog.e(ex.message)
              ExceptionHelper.throwExecution(ErrorMsg.MSG_AGP_VERSION_GET_ERROR)
          }
          println("Android gradle plugin version: $versionValue")

          val version = versionValue as String
          val major = version.substring(0, 1)
          if (major.toInt() < 8) {
              ExceptionHelper.throwExecution(ErrorMsg.MSG_AGP_VERSION)
          }
      }
}

参考源码:

agpVersionChecker.kt

@file:JvmName("AgpVersionChecker")
package com.android.build.gradle.internal.utils
import com.android.Version
import org.gradle.api.Project
import org.gradle.internal.build.IncludedBuildState
/** This is used to enforce the same version for all projects that apply AGP. */
fun enforceTheSamePluginVersions(project: Project) {
    if (project.gradle.parent != null) {
        // This is an included build, do nothing as we'll check from the build that includes it
        return
    }
    val extraProperties = project.rootProject.extensions.extraProperties
    if (extraProperties.has(CHECK_PERFORMED)) {
        return
    }
    val currentVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION
    val currentProjectPath = project.projectDir.canonicalPath
    // all projects in the current build
    project.rootProject.allprojects {
        compareVersions(currentProjectPath, currentVersion, it)
    }
    // all projects in the included build, these are included builds in the composite build
    project.gradle.includedBuilds.forEach { includedBuild ->
        when (includedBuild) {
            is IncludedBuildState -> includedBuild.configuredBuild.allprojects {
                compareVersions(currentProjectPath, currentVersion, it)
            }
            else -> project.logger.warn(
                "Unable to detect AGP versions for included builds. All projects in the build should use the same AGP version. " +
                        "Class name for the included build object: ${includedBuild::class.java.name}."
            )
        }
    }
    extraProperties.set(CHECK_PERFORMED, true)
}
private fun compareVersions(
    firstProjectPath: String,
    firstVersion: String,
    projectToCheck: Project
) {
    val currentProjectPath = projectToCheck.projectDir.canonicalPath
    projectToCheck.plugins.withId("com.android.base") {
        val versionValue = try {
            val versionClass = try {
                it::class.java.classLoader.loadClass(com.android.Version::class.java.name)
            } catch (exception: ClassNotFoundException) {
                // Use deprecated Version class as it exists in older AGP (com.android.Version) does
                // not exist in those versions.
                @Suppress("DEPRECATION")
                it::class.java.classLoader.loadClass(com.android.builder.model.Version::class.java.name)
            }
            val field = versionClass.fields.find { it.name == "ANDROID_GRADLE_PLUGIN_VERSION" }!!
            field.get(null) as String
        } catch (ex: Throwable) {
            projectToCheck.logger.error(
                "Unable to get AGP version for project `$currentProjectPath`. All projects in the build should use the same AGP version.",
                ex
            )
            throw ex
        }
        if (versionValue != firstVersion) throw IllegalStateException(
            """
Using multiple versions of the Android Gradle plugin in the same build is not allowed.
- Project `$firstProjectPath` is using version `$firstVersion` 
- Project `$currentProjectPath` is using version `$versionValue`
            """.trimIndent()
        )
    }
}
private const val CHECK_PERFORMED = "android.agp.version.check.performed"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值