支持 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 nameCorresponding Gradle versions
main6.8.3–6.9.3
gradle707.0
gradle717.1-7.4
gradle757.5
gradle767.6 and higher

In future Kotlin releases, more variants will probably 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 插件变体 - 图1

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】

  1. configurations.register("customConfiguration") {
  2. // ...
  3. }

【Groovy】

  1. configurations.register("customConfiguration") {
  2. // ...
  3. }

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

【Kotlin】

  1. dependencies {
  2. customConfiguration("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10")
  3. }

【Groovy】

  1. dependencies {
  2. customConfiguration 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10'
  3. }

You need to add the following attributes to your customConfiguration:

【Kotlin】

  1. configurations {
  2. customConfiguration {
  3. attributes {
  4. attribute(
  5. Usage.USAGE_ATTRIBUTE,
  6. project.objects.named(Usage.class, Usage.JAVA_RUNTIME)
  7. )
  8. attribute(
  9. Category.CATEGORY_ATTRIBUTE,
  10. project.objects.named(Category.class, Category.LIBRARY)
  11. )
  12. // If you want to depend on a specific KGP variant:
  13. attribute(
  14. GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
  15. project.objects.named("7.0")
  16. )
  17. }
  18. }
  19. }

【Groovy】

  1. configurations {
  2. customConfiguration {
  3. attributes {
  4. attribute(
  5. Usage.USAGE_ATTRIBUTE,
  6. project.objects.named(Usage, Usage.JAVA_RUNTIME)
  7. )
  8. attribute(
  9. Category.CATEGORY_ATTRIBUTE,
  10. project.objects.named(Category, Category.LIBRARY)
  11. )
  12. // If you want to depend on a specific KGP variant:
  13. attribute(
  14. GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
  15. project.objects.named('7.0')
  16. )
  17. }
  18. }
  19. }

Otherwise, you will receive an error similar to this:

  1. > Could not resolve all files for configuration ':customConfiguration'.
  2. > Could not resolve org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0.
  3. Required by:
  4. project :
  5. > Cannot choose between the following variants of org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0:
  6. - gradle70RuntimeElements
  7. - runtimeElements
  8. All of them match the consumer attributes:
  9. - Variant 'gradle70RuntimeElements' capability org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0:
  10. - Unmatched attributes:

下一步做什么?

Learn more about Gradle basics and specifics.