Flutter Gradle Build Failed: Common Causes and Fixes
Running `flutter build apk`, `flutter build appbundle`, or launching on an Android device fails during the Gradle build phase, often with a message like `Execution failed for task ':app:...'` or `Build failed with an exception.`
Execution failed for task ':app:compileDebugJavaWithJavac'.
The Problem
Running `flutter build apk`, `flutter build appbundle`, or launching on an Android device fails during the Gradle build phase, often with a message like `Execution failed for task ':app:...'` or `Build failed with an exception.`
Symptoms
- •Build fails during the Android Gradle build phase, not during Dart compilation
- •Terminal shows `FAILURE: Build failed with an exception.`
- •Error may reference a specific Gradle task, a missing dependency, or a version conflict
Why This Happens
- •Android Gradle Plugin (AGP) version is incompatible with the installed Gradle version
- •compileSdk or minSdk in android/app/build.gradle is set lower than what a plugin requires
- •Java/JDK version installed doesn't match what AGP expects (AGP 8+ requires JDK 17)
- •A stale or corrupted Gradle cache from a previous Flutter/AGP upgrade
- •Conflicting dependency versions pulled in by two different plugins
Quick Fix
Run `flutter clean`, delete the android/.gradle and ~/.gradle/caches directories, then rebuild. This resolves a large share of Gradle failures caused by stale cache state.
How to Fix It
1. Read the actual error above the 'FAILURE' banner
2. Clear Gradle and Flutter build caches
3. Check AGP / Gradle / Java version compatibility
4. Check compileSdk and minSdk
Common Mistakes
- ×Only reading the last 'FAILURE' line instead of scrolling up to the actual error
- ×Upgrading the Android Gradle Plugin without checking the required Gradle and JDK versions
- ×Clearing the wrong cache (e.g. pub cache) when the problem is Gradle-specific
How to Verify the Fix
- 1.Run flutter clean && flutter pub get
- 2.Run flutter build apk (or flutter run) again and confirm it completes without the Gradle failure
- 3.If it still fails, run with --verbose and confirm the actual root-cause line (not the summary) is resolved
Related Problems
Did this article save you time?
I write these for free. If it helped, a coffee keeps me going — and more articles coming.