はじめに
本記事では、個人開発したFlutterプロジェクトを最新バージョンにアップデートする際に実施した作業と、それに伴って発生したエラー・課題、およびその解決策を記録した備忘的位置付けの記事となります。
本記事が、同様のアップデートで課題に直面したエンジニアにとっての参考となることを目的としています。
1. プロジェクトのアップデート概要
今回のプロジェクトアップデートでは、主要な開発環境と依存関係のバージョンを以下の通り更新しました。
| 項目 | アップデート前 | アップデート後 |
|---|---|---|
| Flutter | 3.22.1 | 3.32.0 |
| Android compileSdkVersion | 34 | 35 |
| Gradle | 7.3.1 | 8.9.2 |
また、プロジェクトで使用している主要なDartパッケージも、同時に最新バージョンへ更新を行いました。
(多数のため、メジャーバージョンアップ対応したもののみ記載)
| パッケージ名 | アップデート前 | アップデート後 |
|---|---|---|
| permission_handler | 11.3.1 | 12.0.0 |
| location | 7.0.1 | 8.0.0 |
| flutter_local_notifications | 18.0.1 | 19.2.1 |
| app_settings | 5.1.1 | 6.1.1 |
2. アップデート作業と対応した課題
flutter run コマンド実行中に発生した各エラーに対する具体的な対応内容を以下に記述します。
2.1. Dartコードコンパイル時の課題
エラー1:The argument type 'CardTheme' can't be assigned to the parameter type 'CardThemeData?'.
- 概要:CardTheme 型から CardThemeData? 型への型変換エラー
- 対応:cardTheme の設定において、使用するクラスを CardTheme から CardThemeData に変更
// 修正前
cardTheme: const CardTheme().copyWith(...)
// 修正後
cardTheme: const CardThemeData().copyWith(...)
エラー2:The named parameter 'uiLocalNotificationDateInterpretation' isn't defined.
- 概要:flutter_local_notifications パッケージの zonedSchedule メソッドにおけるパラメータの未定義エラー
- 対応:zonedSchedule メソッドの呼び出しから、uiLocalNotificationDateInterpretation パラメータを削除
// 修正前
await flutterLocalNotificationsPlugin.zonedSchedule(
...,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
...
);
// 修正後
await flutterLocalNotificationsPlugin.zonedSchedule(
...,
// パラメータを削除
...
);
2.2. Android Gradleビルド時の課題
エラー3:Parameter specified as non-null is null: method com.flutter.gradle.VersionUtils.mostRecentSemanticVersion, parameter version1
- 概要:android_alarm_manager_plus パッケージの構成中に発生した、非nullパラメータへのnull渡しエラー(Android Gradle Plugin (AGP) のバージョン不整合に起因)
- 対応:プロジェクトの android/settings.gradle ファイル内のAndroid Gradle Pluginバージョンを、目標バージョンである 8.9.2 に更新
// 修正前
id "com.android.application" version "7.3.1" apply false
// 修正後
id "com.android.application" version "8.9.2" apply false
さらに、android/gradle/wrapper/gradle-wrapper.properties 内のGradleバージョンを 8.11.1 に更新
# 修正前
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
# 修正後
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-all.zip
エラー4:Namespace not specified. Specify a namespace in the module's build file. (:flutter_app_badger)
- 概要:flutter_app_badger パッケージにおいて、Android Gradle Plugin 8.0以降で必須となった namespace が未指定
- 対応:flutter_app_badger パッケージのPubキャッシュ内にある android/build.gradle ファイルを直接編集し、android { ... } ブロック内に namespace を追記
android {
namespace "fr.g123k.flutterappbadge.flutterappbadger"
// ...
}
エラー5:Namespace not specified. Specify a namespace in the module's build file. (:image_gallery_saver)
- 概要:image_gallery_saver パッケージにおいて、エラー4と同様の namespace 未指定エラー
- 対応:image_gallery_saver パッケージのPubキャッシュ内にある android/build.gradle ファイルを直接編集し、android { ... } ブロック内に namespace を追記
android {
namespace "com.example.imagegallerysaver"
// ...
}
エラー6:Dependency ':flutter_local_notifications' requires desugar_jdk_libs version to be 2.1.4 or above for :app, which is currently 1.2.2
- 概要:flutter_local_notifications パッケージが、より新しい desugar_jdk_libs (バージョン2.1.4以上)を要求しているにも関わらず、プロジェクトでは古いバージョン(1.2.2)を使用しているエラー
- 対応:プロジェクトのメインの android/app/build.gradle ファイル内の dependencies ブロックで、desugar_jdk_libs のバージョンを 2.1.4 に更新
// 修正前
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
// 修正後
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'
エラー7:Inconsistent JVM-target compatibility detected for tasks 'compileDebugJavaWithJavac' (1.8) and 'compileDebugKotlin' (17).
- 概要:image_gallery_saver パッケージのコンパイルにおいて、JavaとKotlinのターゲットJVMバージョンがそれぞれ1.8と17で一致していないエラー
- 対応:image_gallery_saver パッケージのPubキャッシュ内にある android/build.gradle ファイルを編集し、android { ... } ブロック内の compileOptions および kotlinOptions を更新し、両方のJVMターゲットを 17 に揃える
android {
// ...
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
// ...
}
エラー8:Unresolved reference: Registrar (:image_gallery_saver)
- 概要:image_gallery_saver パッケージの ImageGallerySaverPlugin.kt ファイル内で、Registrar という参照が解決できないエラー
- 対応:image_gallery_saver パッケージのPubキャッシュ内にある android/src/main/kotlin/com/example/imagegallerysaver/ImageGallerySaverPlugin.kt ファイルを直接編集し、不要な import io.flutter.plugin.common.PluginRegistry.Registrar の行を削除
3. まとめ
今回対応したFlutterプロジェクトのアップデートでは、Flutter、Android Gradle Plugin(AGP)、および依存するパッケージのバージョン更新に伴う複数の課題に直面しました。
これらの課題に対し、各エラーメッセージを詳細に分析し、影響を受けるファイルや設定箇所を特定することで、段階的に解決を進めました。
特に、一部のサードパーティライブラリでは、最新のFlutter/Android環境への適応が遅れているためか、ライブラリ固有のビルド設定やコードの修正を一時的に行う必要がありました。
やはり、ライブラリの更新は頻繁に行なっておく必要があると改めて感じました。
本記事の内容を通じて得られた知見が、今後の類似プロジェクトにおけるトラブルシューティングの参考になることを期待します。