【Flutter】Kotlin Gradle Pluginのバージョンエラーの解決方法
Kotlin Gradle Plugin のバージョンが古いため、Flutterが求めるバージョンに更新する必要があるというエラーがでました。このときのFlutterのバージョンはv3.27.3でした。
─ Flutter Fix ──────────────────────────────────────────┐
│ [!] Your project requires a newer version of the │
│ Kotlin Gradle plugin. │
│ Find the latest version on │
│ https://kotlinlang.org/docs/releases.html#release-deta │
│ ils, then update the │
│ version number of the plugin with id │
│ "org.jetbrains.kotlin.android" in the plugins block of │
│ │
│ /Users/mac/flutter_project/habit_app/android/settings. │
│ gradle. │
│ │
│ Alternatively (if your project was created before │
│ Flutter 3.19), update │
│ /Users/mac/flutter_project/habit_app/android/build.gra │
│ dle │
│ ext.kotlin_version = '<latest-version>' │
このエラーの解決のため、Kotlin のバージョンを更新しました。
Kotlinの最新バージョンを確認
Kotlinの最新バージョンに更新。以下の公式から確認できます。
android/settings.gradle
の修正:
plugins {
***省略***
id "org.jetbrains.kotlin.android" version "2.1.10" apply false
***省略***
}
修正するとKotlin2.0以降ではComposeを使用する場合、Compose Compiler Gradle プラグインが必須というエラーがでました。
Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required
Jetpack Compose と Compose Compiler とは、Flutterの Flutter SDK と Dart コンパイラ みたいな関係だと思うと理解しやすそうです。
- Jetpack Compose : UIフレームワーク(ボタン、テキスト、レイアウトを作る)
- Compose Compiler : KotlinコードをUIに変換するコンパイラ
Compose と Kotlin の互換性マップは以下から確認できました。たしかにKotlin2.0.0以降はCompose Compiler Gradle プラグインを使用してComposeを有効にすると書いてあります。
Compose Compiler Gradle プラグインを使用してComposeを有効にする
settings.gradle の plugins セクションに Compose Plugin を追加
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.6.0" apply false
id "org.jetbrains.kotlin.android" version "2.1.10" apply false
id "org.jetbrains.kotlin.plugin.compose" version "2.1.10" apply false // 追加
id "com.google.gms.google-services" version "4.4.0" apply false
id "com.google.firebase.crashlytics" version "2.9.9" apply false
}
android/app/build.gradle の plugins にも id "org.jetbrains.kotlin.plugin.compose" を追加
plugins {
id 'com.android.application'
id "kotlin-android"
id "org.jetbrains.kotlin.plugin.compose" // 追加
id "dev.flutter.flutter-gradle-plugin"
id "com.google.gms.google-services"
}
composeOptionsのCompilerのバージョンを最新にする
composeOptions {
kotlinCompilerExtensionVersion = "1.5.15"
}
dependenciesにあるactivity-composeのバージョンも更新する
dependencies {
***省略***
implementation "androidx.activity:activity-compose:1.5.15" //バージョン更新
***省略***
}
古い activity-compose だと、新しい Kotlin や Compose Compiler に対応していないため、バージョンを上げる必要があるようでした。
エラーが無事解消されました!
Discussion