📳

【Flutter】突然動かなくなった!→AGPのバージョンが古い!その場合のビルドエラーと解決方法

2025/02/12に公開

一か月くらい放置していたFlutterのプロジェクトを動かそうと思ったら
急に変な警告が出て動かないことってありますよね(多分そんなにない)

ということで、今回は、Flutterのプロジェクトをデバッグしようとしたときに、
「FAILURE: Build failed with an exception.」
が起きた時に、私が行った対処方法についてです。

起きたエラー

VS CodeでFlutterプロジェクトのデバッグを
Windows→Macに環境を移して実行しようとしたところ、
以下のようなエラーが出ました。

Launching lib/main.dart on sdk gphone64 arm64 in debug mode...
Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':sqflite_android:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':sqflite_android:androidJdkImage'.
   > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JdkImageTransform: /Users/xxxxxxxxxx/Library/Android/sdk/platforms/android-34/core-for-system-modules.jar.
         > Error while executing process /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/jlink with arguments {--module-path /Users/xxxxxxxxxx/.gradle/caches/transforms-3/9fb51353889e697c585cdd0167b151a4/transformed/output/temp/jmod --add-modules java.base --output /Users/xxxxxxxxxx/.gradle/caches/transforms-3/9fb51353889e697c585cdd0167b151a4/transformed/output/jdkImage --disable-plugin system-modules}
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 8s
┌─ Flutter Fix ─────────────────────────────────────────
│ [!] This is likely due to a known bug in Android Gradle Plugin (AGP) versions less than 8.2.1,  
│ when                                                                              
│   1. setting a value for SourceCompatibility and                                  
│   2. using Java 21 or above.                                                      
│ To fix this error, please upgrade your AGP version to at least 8.2.1. The version of AGP that   
│ your project uses is likely defined in:                                           
│ /Users/xxxxxxxxxx/Project/flutter_application_1/android/settings.gradle,          
│ in the 'plugins' closure (by the number following "com.android.application").     
│  Alternatively, if your project was created with an older version of the templates, it is likely
│ in the buildscript.dependencies closure of the top-level build.gradle:            
│ /Users/xxxxxxxxxx/Project/flutter_application_1/android/build.gradle,             
│ as the number following "com.android.tools.build:gradle:".                       
│                                                                                  
│ For more information, see:                                                       
│ https://issuetracker.google.com/issues/294137077                                 
│ https://github.com/flutter/flutter/issues/156304                                 
└────────────────────────────────────────────────
Error: Gradle task assembleDebug failed with exit code 1
Exited (1).

英語が不得意な私には怪文書にしか見えません。
要点としては、

  • FAILURE: Build failed with an exception.→ビルドのエラーが起きている
  • Flutter Fix→ 診断情報/警告メッセージが表示されている
    ということです。

今回の場合は、もろもろのエラー解析をAIにも手伝ってもらい、
「AGPのバージョンが8.2.1未満であり、Java 21以上を使用している」ことが原因
ということがわかりました。

具体的な修正箇所

android/settings.gradleの

plugins {
    id "com.android.application" version "8.1.0" apply false
}

plugins {
    id 'com.android.application' version '8.2.1' apply false
}

と、バージョンを変更するだけで、エラーを解消することができました。

おしまいに

エラーをAIに投げると大枠でいい感じの答えを返してくれるので、
本当に新しい技術へのハードルが下がったなと実感いたします。

Discussion