Skip to content

Gradleプラグインバリアントのサポート

Gradle 7.0では、Gradleプラグインの作成者向けに新しい機能「バリアントを持つプラグイン」が導入されました。 この機能により、古いGradleバージョンとの互換性を維持しながら、最新のGradle機能のサポートをより簡単に追加できるようになります。 Gradleにおけるバリアント選択についての詳細をご確認ください。

Gradleプラグインバリアントを使用することで、Kotlinチームは異なるGradleバージョンに対して異なるKotlin Gradleプラグイン(KGP)バリアントを提供できます。 その目的は、サポートされている最も古いバージョンのGradleに対応する main バリアントで基本的なKotlinコンパイルをサポートすることです。各バリアントは、対応するリリースのGradle機能の実装を持ちます。最新のバリアントは最新のGradle機能セットをサポートします。このアプローチにより、機能は制限されるものの、より古いGradleバージョンのサポートを継続することが可能になります。

現在、Kotlin Gradleプラグインには以下のバリアントがあります:

バリアント名対応するGradleバージョン
main7.6.0–7.6.3
gradle808.0–8.0.2
gradle818.1.1
gradle828.2.1–8.4
gradle858.5
gradle868.6-8.7
gradle888.8-8.10
gradle8118.11-8.12
gradle8138.13 以降

今後のKotlinのリリースでは、さらに多くのバリアントが追加される予定です。

ビルドでどのバリアントが使用されているかを確認するには、--info ログレベルを有効にし、出力の中から Using Kotlin Gradle plugin で始まる文字列(例:Using Kotlin Gradle plugin main variant)を探してください。

トラブルシューティング

以下は、Gradleにおけるバリアント選択に関する既知の問題の回避策です:

GradleがカスタムコンフィギュレーションでKGPバリアントを選択できない

Gradleがカスタムコンフィギュレーション(Custom Configuration)においてKGPバリアントを選択できないのは、想定されている挙動です。 カスタムGradleコンフィギュレーションを使用している場合:

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

そして、Kotlin Gradleプラグインへの依存関係を追加したい場合(例):

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

その 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)
            )
            // 特定のKGPバリアントに依存したい場合:
            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)
            )
            // 特定のKGPバリアントに依存したい場合:
            attribute(
                GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
                project.objects.named('7.0')
            )
        }
    }
}

そうしないと、以下のようなエラーが発生します:

none
 > 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:

次のステップ

Gradleの基本と詳細について詳しく学びましょう。