FlutterでAGPのバージョンが古い場合のビルドエラーと解決方法

FlutterでAGPのバージョンが古い場合のビルドエラーについて、解説します。

AGPのバージョンが古い場合のビルドエラー

以下のエラーは、Android Gradle Plugin (AGP) のバージョンが古いことが原因です。
発生しています。
具体的には、Flutter Fix に記載されているとおり「AGPのバージョンが8.2.1未満であり、Java 21以上を使用している」ことが原因です。

Running Gradle task 'assembleDebug'...
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 ':path_provider_android:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':path_provider_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: C:\Users\XXX\AppData\Local\Android\sdk\platforms\android-34\core-for-system-modules.jar.
         > Error while executing process C:\Program Files\Android\Android Studio\jbr\bin\jlink.exe with arguments {--module-path C:\Users\XXX\.gradle\caches\transforms-3\xxxxxxxxxxxxxxxxxxxx\transformed\output\temp\jmod --add-modules java.base --output C:\Users\XXX\.gradle\caches\transforms-3\xxxxxxxxxxxxxxxxxxxx\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 1m 17s

┌─ 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:                                                          │
│ C:\github\flutter\xxx\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:                           │
│ C:\github\flutter\xxx\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

エラーの解決方法

解決方法は以下のとおりです。

①プロジェクトのsettings.gradleファイルまたはbuild.gradleファイルを開き、AGPのバージョンを8.2.1以上に更新します。

  • 例えば、以下のように変更します。
     buildscript {
         dependencies {
             classpath 'com.android.tools.build:gradle:8.2.1'
         }
     }

② 以下のコマンドを実行して、Gradleのキャッシュをクリアします。

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\github\flutter\reki\android\app\build.gradle' line: 19

* What went wrong:
A problem occurred evaluating project ':app'.
> Unknown Kotlin JVM target: 21

* 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 1s
Error: Gradle task assembleDebug failed with exit code 1

これで、プロジェクトを再度ビルドすればエラーが解決されるはずです。

【Flutter超入門】使い方とサンプルアプリを解説
Flutterの使い方とサンプルアプリ(iOS、Android、Windows)について入門者向けに解説します。

コメント