支持 Gradle 插件变体

Gradle 7.0 introduced a new feature for Gradle plugin authors — plugins with variants. This feature makes it easier to add support for latest Gradle features while maintaining compatibility with older Gradle versions. Learn more about variant selection in Gradle.

With Gradle plugin variants, the Kotlin team can ship different Kotlin Gradle plugin (KGP) variants for different Gradle versions. The goal is to support the base Kotlin compilation in the main variant, which corresponds to the oldest supported versions of Gradle. Each variant will have implementations for Gradle features from a corresponding release. The latest variant will support the latest Gradle feature set. With this approach, it is possible to extend support for older Gradle versions with limited functionality.

Currently, there are the following variants of the Kotlin Gradle plugin:

Variant's name Corresponding Gradle versions
main 6.8.3–6.9.3
gradle70 7.0
gradle71 7.1-7.4
gradle75 7.5
gradle76 7.6
gradle80 8.0
gradle81 8.1.1 and higher

In future Kotlin releases, more variants will be added.

To check which variant your build uses, enable the --info log level and find a string in the output starting with Using Kotlin Gradle plugin, for example, Using Kotlin Gradle plugin main variant.

问题排查

Here are workarounds for some known issues with variant selection in Gradle:

Gradle 无法在自定义配置中选择 KGP 变体

This is an expected situation that Gradle can't select a KGP variant in a custom configuration. If you use a custom Gradle configuration:


【Kotlin】

configurations.register("customConfiguration") {
    // ...
}

【Groovy】

configurations.register("customConfiguration") {
    // ...
}

And want to add a dependency on the Kotlin Gradle plugin, for example:


【Kotlin】

dependencies {
    customConfiguration("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24")
}

【Groovy】

dependencies {
    customConfiguration 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24'
}

You need to add the following attributes to your customConfiguration:


【Kotlin】

configurations {
    customConfiguration {
        attributes {
            attribute(
                Usage.USAGE_ATTRIBUTE,
                project.objects.named(Usage.class, Usage.JAVA_RUNTIME)
            )
            attribute(
                Category.CATEGORY_ATTRIBUTE,
                project.objects.named(Category.class, Category.LIBRARY)
            )
            // If you want to depend on a specific KGP variant:
            attribute(
                GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
                project.objects.named("7.0")
            )
        }
    }
}

【Groovy】

configurations {
    customConfiguration {
        attributes {
            attribute(
                Usage.USAGE_ATTRIBUTE,
                project.objects.named(Usage, Usage.JAVA_RUNTIME)
            )
            attribute(
                Category.CATEGORY_ATTRIBUTE,
                project.objects.named(Category, Category.LIBRARY)
            )
            // If you want to depend on a specific KGP variant:
            attribute(
                GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
                project.objects.named('7.0')
            )
        }
    }
}

Otherwise, you will receive an error similar to this:

 > Could not resolve all files for configuration ':customConfiguration'.
      > Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0.
        Required by:
            project :
         > Cannot choose between the following variants of org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0:
             - gradle70RuntimeElements
             - runtimeElements
           All of them match the consumer attributes:
             - Variant 'gradle70RuntimeElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0:
                 - Unmatched attributes:

下一步做什么?

Learn more about Gradle basics and specifics.