Kotlin pluginとかkotlin-gradle-pluginとかややこしいので整理する

この記事をシェア

Kotlinは今まさに成長中の言語ですので、どんどん新機能が追加されバージョンも新しくなっていっています。それ自体は大歓迎なのですが、Androidアプリを開発していると「Kotlinナントカプラグイン」みたいなのがいくつか出てきて、どれを更新したら何に影響するのか、よくわかりません。

そこで今回は、Androidアプリ開発に必要なKotlin関連プラグインやライブラリなどのバージョンについて整理したいと思います。

Kotlin plugin

Kotlin pluginはAndroid StudioやIntelliJ IDEAでKotlinを使うために必要なプラグインです。Android Studioをインストールすると初めからEnabledになっています。

リリースサイクルとバージョン

Kotlin pluginはKotlinのリリースやIntelliJ IDEAのリリースに合わせてリリースされます。

The Kotlin IDE plugin will be released simultaneously with Kotlin, and on top of that — every time IntelliJ IDEA is released.

https://blog.jetbrains.com/kotlin/2020/10/new-release-cadence-for-kotlin-and-the-intellij-kotlin-plugin/

The IntelliJ Kotlin plugin and IntelliJ IDEA are on the same release cycle.

https://kotlinlang.org/docs/plugin-releases.html

Kotlin pluginは、ひとつ前のバージョンのKotlinコンパイラに対応しています。つまり、Kotlin 1.6.0を使いたければプラグインを1.6.10にするいう必要があるようです。

The Kotlin plugin is based on the previous stable version of the Kotlin compiler. 

https://kotlinlang.org/docs/plugin-releases.html

しかしこのリリース方針はKotlinコミュニティ内でも混乱が生じたようで、Kotlin公式ブログで謝罪するとともに2022年以降はプラグインは最新バージョンのコンパイラでリリースすると表明しています。

This situation caused a lot of confusion in the community. We would like to apologize for the current situation and answer all of your questions regarding Kotlin support in IntelliJ IDEA to help you plan Kotlin and IDE updates for your projects:

https://blog.jetbrains.com/kotlin/2021/12/kotlin-1-6-support-in-intellij-idea-2021-3/

All future IntelliJ IDEA versions, starting with 2022.1, will be released simultaneously with the Kotlin IDE Plugin based on the latest stable compiler version.

https://blog.jetbrains.com/kotlin/2021/12/kotlin-1-6-support-in-intellij-idea-2021-3/

まとめ

まとめると、Kotlin pluginはAndroid StudioでKotlinを使うために必須のプラグインで、今はちょっと混乱しているものの、基本的にはKotlinのリリースに合わせて更新されるので、最新のKotlinを使いたければ最新のKotlin pluginに更新します。

ちなみにKotlin plugin 1.6.10に更新した時にちょっと苦労したいきさつは、下記の記事に公開しています。

Kotlin Gradle Plugin

Kotlin gradle pluginはGradleでKotlinをビルドするためのGradleプラグインです。

・・・全く説明になっていないですね。Gradleは、それ自体にはソースコードをビルドしたりする機能はなく、そういった機能はプラグインで実現しているそうです。

Gradleのコア部分には、実際の自動化に役に立つような機能は含まれていません。 これは意図的なもので、Javaのコードをコンパイルしたりといった便利な機能は、全てプラグインにより追加されます。 プラグインは、コア部分のオブジェクトや他のプラグインのオブジェクトを拡張するだけでなく、新しいタスク(JavaCompileなど)やドメインオブジェクト(SourceSetなど)、規約(Javaのコードはsrc/main/javaに置く)、などをGradleに追加します。

http://gradle.monochromeroad.com/docs/userguide/plugins.html

なので、KotlinをGradleでビルドするにはKotlin gradle pluginが必要、ということになります。

バージョン

Kotlin gradle pluginのバージョンはプロジェクトのbuild.gradleで指定します。

buildscript {
    ext {
        compose_version = '1.0.5'
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
    }
}

Kotlin gradle pluginのバージョンに関して詳しい情報を見つけることはできませんでしたが、おそらくここで指定したバージョンはKotlinコンパイラのバージョンを示していると思われます。根拠は以下の「Kotlinプラグインはひとつ前のバージョンのコンパイラに基づくが、プロジェクトのKotlinのバージョンはアップデートできるよ」という記述です。Kotlin version in your projectという部分が、kotlin-gradle-pluginを意味するのかな、と解釈しました。

The Kotlin plugin is based on the previous stable version of the Kotlin compiler. You can still update the Kotlin version in your project, but some IDE-related features might not be available. We are working on stabilizing the process so that the next versions of the plugin will be based on the latest stable version of the compiler.

https://kotlinlang.org/docs/plugin-releases.html#update-to-a-new-release

バージョン更新してみた

build.gradleを書き換えてKotlin gradle pluginのバージョンを変更すると、Migrationの通知が出ました。

おそるおそるOKを押してみましたが、今回は特に何も変更すべきコードは見つからなかったようです。

ライブラリが対応していないバージョンを指定するとエラーになる

Kotlin gradle pluginのバージョン更新自体は、build.gradleのバージョン番号を書き換えるだけなので簡単なのですが、注意点としては、プロジェクトが使用しているライブラリが対応していないKotlinのバージョンを指定すると、エラーが出ます。

例えば、本記事執筆時点(2021/12/27)のKotlin gradle pluginの最新バージョンは1.6.10ですが、Jetpack Composeの最新バージョン1.0.5はKotlin 1.5.31に依存しています。そのため、Jetpack Composeを使用しているプロジェクトでKotlin gradle pluginのバージョンを1.6.10にすると、次のようなエラーが出ます。

e: This version (1.0.5) of the Compose Compiler requires Kotlin version 1.5.31 but you appear to be using Kotlin version 1.6.10 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don’t say I didn’t warn you!).

エラーメッセージにある通り、Jetpack Compose 1.0.5はKotlin 1.5.31に依存しています。一応Jetpack Composeのリリースノートを見て確認してみました。Jetpack Composeは複数のライブラリで構成されていますが、バージョンは統一してあります。Compose Compilerのリリースノートを見てみると、1.0.4の説明に以下のように書かれていました。

Kotlin 1.5.31 に依存するように更新しました。

https://developer.android.com/jetpack/androidx/releases/compose-compiler#1.0.4

まとめ

Kotlin gradle pluginはbuild.gradleでバージョンを指定し、個別のプロジェクトのビルドに使うKotlinのバージョンを指定します。ライブラリの対応状況によってKotlinバージョンを選択する必要があるため、Kotlin pluginのバージョンからは独立して指定できるようになっています。ライブラリが対応していないバージョンのKotlin gradle pluginを使うとビルドエラーになるので注意が必要です。

composeOptions.kotlinCompilerVersion

Jetpack Composeのプロジェクトを作成すると、build.gradle(:app)に自動で以下のようにkotlinComplierVersionが宣言されます。

android {
    ...

    composeOptions {
        kotlinCompilerExtensionVersion compose_version
        kotlinCompilerVersion '1.5.21'
    }

    ...
}

しかしこれはDeprecatedです。ここでバージョンを指定しても無視され、Kotlin gradle plugin(上で説明したやつです)のバージョンを参照するそうです。そうしないと別のバージョンを指定されたときに整合性が取れなくなってしまいますからね。

ということでkotlinCompilerVersionの行は削除してしまって大丈夫です。

まとめ

今回は、Kotlin plugin, Kotlin gradle plugin, composeOptions.kotlinCompilerVersionの3つの意味やバージョンのつけ方などを整理しました。これらの意味を理解していれば、バージョン更新も安心して行うことができると思います。Kotlinは次々に便利な機能が追加されていますので、積極的に新しいバージョンを取り入れていきたいですね。

この記事をシェア