Gradle プラグインのバリアントのサポート
Gradle 7.0 では、Gradle プラグインの作成者向けに新機能であるバリアントを持つプラグインが導入されました。この機能により、以前の Gradle バージョンとの互換性を維持しながら、最新の Gradle 機能のサポートを簡単に追加できるようになります。Gradle のバリアント選択について詳しくはこちらをご覧ください。
Gradle プラグインのバリアントを使用すると、Kotlin チームはさまざまな Gradle バージョン向けに異なる Kotlin Gradle プラグイン (KGP) のバリアントを提供できます。目標は、Gradle の最も古いサポート対象バージョンに対応する main
バリアントで、Kotlin の基本的なコンパイルをサポートすることです。各バリアントには、対応するリリースからの Gradle 機能の実装が含まれます。最新のバリアントは、最新の Gradle 機能セットをサポートします。このアプローチにより、限定された機能ではあるものの、古い Gradle バージョンのサポートを拡張することが可能です。
現在、Kotlin Gradle プラグインには以下のバリアントがあります。
バリアント名 | 対応する Gradle バージョン |
---|---|
main | 7.6.0–7.6.3 |
gradle80 | 8.0–8.0.2 |
gradle81 | 8.1.1 |
gradle82 | 8.2.1–8.4 |
gradle85 | 8.5 and higher |
今後の Kotlin リリースでは、さらに多くのバリアントが追加される予定です。
ビルドがどのバリアントを使用しているかを確認するには、--info
ログレベルを有効にし、出力で Using Kotlin Gradle plugin
から始まる文字列を探します。例えば、Using Kotlin Gradle plugin main variant
のようになります。
トラブルシューティング
NOTE
Gradle のバリアント選択に関するいくつかの既知の問題に対する回避策を以下に示します。
カスタム設定で KGP バリアントを Gradle が選択できない
これは、カスタム設定で Gradle が KGP バリアントを選択できないという、予期される状況です。 カスタム Gradle 設定を使用している場合:
configurations.register("customConfiguration") {
// ...
}
configurations.register("customConfiguration") {
// ...
}
そして、例えば Kotlin Gradle プラグインに依存関係を追加したい場合:
dependencies {
customConfiguration("org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.21")
}
dependencies {
customConfiguration 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.1.21'
}
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)
)
// 特定の KGP バリアントに依存したい場合:
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)
)
// 特定の KGP バリアントに依存したい場合:
attribute(
GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE,
project.objects.named('7.0')
)
}
}
}
そうしないと、次のようなエラーが表示されます。
> 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 の基本と詳細について詳しくはこちらをご覧ください。