Kotlin v2.0.21 Help

Support for Gradle plugin variants

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.3

gradle74

7.4

gradle75

7.5

gradle76

7.6

gradle80

8.0

gradle81

8.1.1

gradle82

8.2.1–8.4

gradle85

8.5 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.

Troubleshooting

Gradle can't select a KGP variant in a custom configuration

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

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

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

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

You need to add the following attributes to your customConfiguration:

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") ) } } }
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:

What's next?

Learn more about Gradle basics and specifics.

Last modified: 26 十一月 2024