Flutter Won’t Build My APK

How to get around Flutter’s irritating Android configuration issues.

FlutterAndroid

If you’ve been trying to build your Flutter-based-Android app for the last few days without success. Welcome to the club.

If you’re seeing output like this:

bash
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\YourName\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 {... long arguments...}

Then let me help you resolve the problem. I don’t know enough about Flutter development to say if this fix will always work, so I’m sorry if it doesn’t help you. I’ll keep it brief so you don’t waste time.

Step 1 — Figure out your Flutter Config

You can do this by using:

bash
flutter doctor -v

This should spit out information like this for you:

bash
[√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.19045.5247], locale en-GB)
    • Flutter version 3.24.3 on channel stable at C:\Users\<YourName>\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (4 months ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at C:\Users\<YourName>\AppData\Local\Android\sdk
    • Platform android-34, build-tools 33.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

The part we’re interested in is the Android toolchain. My Java version is set to OpenJDK 21. This is too high for Flutter, which recommends OpenJDK 17.

How I knew this was the issue

While figuring out what was wrong, I tried running the Gradle build manually. I did this by entering the /android directory of the app, then running:

bash
./gradlew assembleDebug

This ran fine! 🤔 So, I checked the version of Java being used with:

bash
./gradlew -version

Lo and behold, I was on OpenJDK 17:

bash
------------------------------------------------------------
Gradle 8.11
------------------------------------------------------------

Build time:    2024-11-11 13:58:01 UTC
Revision:      b2ef976169a05b3c76d04f0fa76a940859f96fa4

Kotlin:        2.0.20
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  17.0.11 (Amazon.com Inc. 17.0.11+9-LTS)
Daemon JVM:    C:\Program Files\Amazon Corretto\jdk17.0.11_9 (no JDK specified, using current Java home)
OS:            Windows 10 10.0 amd64

For some reason Flutter overrides the JAVA_HOME environment variable and points at the Android Studio Java version. It’s unclear how to change this version in Android Studio (I tried, believe me), so that leaves one option.

Step 2 — Point Flutter at OpenJDK 17

I’m presuming you already have OpenJDK 17 installed. If not, you can install it here:

OpenJDK Download - Corretto - AWS

Pointing Flutter at this version is the fix that worked for me. You can do this with the following command from your terminal in the project’s root directory:

bash
flutter config --jdk-dir "C:/path/to/your/jdk/version/here"

You only need to point at the folder. Check this worked by running flutter doctor again:

bash
flutter doctor -v

Try recompiling your app, and it should all run fine. Hurray!

Final Words

I hope this helps you. Have a great time developing apps! Let me know in the comments if you have any issues, and I’ll try and help.