Closed6

[Flutter] Gradleアップデート時に起こった困ったこと、およびその解消までの道のり

Hideyuki FujiharaHideyuki Fujihara

開発しているFlutterアプリ内でKotlinやJavaのコードを参照している箇所があり、

そこで使用されているGradleのバージョンを7系から8系に上げた際に起こったエラーと、

その解消までの道のりを記します。

Hideyuki FujiharaHideyuki Fujihara

出力されたエラーたちはこんな感じです。

The supplied phased action failed with an exception.
A problem occurred configuring root project 'android'.
A problem occurred evaluating root project 'android'.
A problem occurred configuring project ':app'.
Could not create an instance of type com.android.build.api.variant.impl.ApplicationVariantImpl.
Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.
If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant

上記エラーは先述のGradleに依存する箇所で発生していました。

また、これに関連するJavaコードではこんなエラーも発生していました。

The import androidx cannot be resolved
The import io.flutter.Log cannot be resolved
The import io.flutter.embedding cannot be resolved
Keep cannot be resolved to a type
NonNull cannot be resolved to a type
FlutterEngine cannot be resolved to a type
jp cannot be resolved to a type
Log cannot be resolved

さらに一部のbuild.gradleではこんなエラーも発生していた。

unable to resolve class GradleException
@ line 11, column 15.

ただし、これらが発生していてもdebug buildは実行できました。

Hideyuki FujiharaHideyuki Fujihara

そこで環境が壊れていないか以下のコマンドを実行しました。

flutter doctor -v

ただし、

• No issues found!

このように特に問題は出力されませんでした。

Hideyuki FujiharaHideyuki Fujihara

そこでこちらの記事を参考にさせていただきました。

https://scrapbox.io/yuki-2021/Flutterでandroidのビルドキャッシュを削除する方法

「4. Gradleキャッシュの削除:」以外のすべてのプロセスを頭から順番に実行してみて、

使用していたエディッターを閉じて再度立ち上げ直してみました。

するといくつかのエラーは解消されたのですが、全ては消えてはくれませんでした。

Hideyuki FujiharaHideyuki Fujihara

最終的には以下の拡張機能を無効化してみました。

すると全てのエラーが解消されていました🙌

恐らくはこの拡張機能がプロジェクト内のGradleのバージョンをカバーしていなかったことが

原因だったのでしょう。

拡張機能まわりもこういったエラーを発生させ得ることを学べたので、

これはこれで良かったのかも知れませんね。

Hideyuki FujiharaHideyuki Fujihara

ただその後、このGradleアップデートを含まないブランチにチェックアウトしたところ、

同じエラーが発生してしまいました。。。

上記のアップデートをこのブランチに取り込んでいなかったことに

起因していたみたいだったので、

ローカルマージをして対応したところ、

無事エラーは解消されました🙌

.....

依存関係まわりのアップデートは塩漬けすべきではないですが、

いかんせんやっかいなことも多いですね😅

このスクラップは3ヶ月前にクローズされました