Build Failure Diagnosis and Fix Skill
Expert guidance for diagnosing and fixing Flutter build failures in Android projects, including dependency conflicts, Gradle errors, and compilation issues.
When to Use This Skill
- •Flutter build fails (
flutter build apkorflutter build appbundle) - •Gradle sync/build errors
- •Dependency resolution failures
- •Compilation errors (Dart or Kotlin/Java)
- •Native build failures
- •Plugin integration issues
- •Version conflicts
Build Failure Diagnostic Workflow
Step 1: Identify the Build Stage
Flutter Android builds go through several stages:
- •Dependency Resolution (
flutter pub get) - •Dart Compilation (
flutter build) - •Gradle Configuration (Android build system setup)
- •Resource Processing (R8, ProGuard, resources)
- •Native Compilation (Kotlin/Java code)
- •Packaging (APK/AAB creation)
Identify which stage is failing to narrow down the issue.
Step 2: Clean Build First
Always try a clean build first:
bash
# Nuclear option - clean everything flutter clean cd android ./gradlew clean cd .. rm -rf ~/.gradle/caches # Only if really stuck flutter pub get flutter build apk --verbose
Step 3: Analyze Error Messages
Look for key error patterns:
- •Gradle: "BUILD FAILED", "Could not resolve", "Deprecated"
- •Dart: "Error:", "Warning:", "Unhandled exception"
- •R8/ProGuard: "shrinking", "Missing class"
- •Dependencies: "version conflict", "incompatible"
Common Build Failures and Solutions
1. Dependency Conflicts
Issue: Version Conflicts
code
Error: Version conflict between dependencies
Diagnosis:
bash
cd android ./gradlew app:dependencies
Solution:
- •Review
pubspec.yaml- update conflicting dependencies - •Check
android/app/build.gradle.ktsfor Android dependency versions - •Use version ranges carefully:
^1.0.0vs>=1.0.0 <2.0.0 - •Run
flutter pub upgrade --major-versionsif needed
Issue: Missing Dependencies
code
Error: Could not find dependency
Solution:
- •Check dependency name spelling in
pubspec.yaml - •Verify dependency exists on pub.dev
- •Check internet connection
- •Clear pub cache:
flutter pub cache repair
2. Gradle Build Errors
Issue: Gradle Version Incompatibility
code
Error: Gradle version X.X is required
Solution:
- •Update Gradle wrapper:
cd android && ./gradlew wrapper --gradle-version=8.0 - •Check
android/gradle/wrapper/gradle-wrapper.properties - •Ensure Java 17 is installed:
java -version
Issue: Gradle Daemon Issues
code
Error: Gradle daemon stopped unexpectedly
Solution:
- •Stop daemon:
cd android && ./gradlew --stop - •Clear Gradle cache:
rm -rf ~/.gradle/caches - •Check memory settings in
gradle.properties - •For CI, use
android/gradle-ci.properties
Issue: Deprecated Gradle Configuration
code
Warning: The following Gradle features are deprecated
Solution:
- •Review
android/build.gradle.ktsandandroid/app/build.gradle.kts - •Update Gradle plugin version
- •Check for deprecated DSL usage
- •Run
./gradlew --warning-mode allfor details
3. Java/JVM Issues
Issue: Wrong Java Version
code
Error: Compilation failed: java.lang.UnsupportedClassVersionError
Solution:
- •This project requires Java 17
- •Check:
java -version - •Set JAVA_HOME:
export JAVA_HOME=/path/to/java17 - •Verify in gradle files:
JavaVersion.VERSION_17
Issue: Out of Memory
code
Error: GC overhead limit exceeded
Solution:
- •Increase heap size in
android/gradle.properties:propertiesorg.gradle.jvmargs=-Xmx4g -XX:MaxMetaspaceSize=1g
- •For CI, use
android/gradle-ci.properties(3GB limit) - •Enable parallel GC:
-XX:+UseParallelGC
4. R8/ProGuard Issues
Issue: Class Not Found After R8 Shrinking
code
Error: Missing class referenced from: ...
Solution:
- •Add ProGuard rules in
android/app/proguard-rules.pro:proguard-keep class com.your.package.** { *; } -dontwarn com.problematic.library.** - •Check if library provides ProGuard rules
- •Disable shrinking temporarily to test:
shrinkResources false
Issue: R8 Fails
code
Error: R8 task failed
Solution:
- •Update Android Gradle Plugin
- •Check for incompatible ProGuard rules
- •Try R8 full mode:
android.enableR8.fullMode=true - •Use
--verboseflag to see details
5. Native Code Issues
Issue: Kotlin Compilation Fails
code
Error: Kotlin compilation failed
Solution:
- •Check Kotlin version in
android/build.gradle.kts - •Review Kotlin code in
android/app/src/main/kotlin/ - •Ensure Kotlin plugin version matches Gradle
- •Update to latest stable Kotlin version
Issue: NDK/Native Library Issues
code
Error: Could not find NDK
Solution:
- •Install NDK via Android Studio or SDK Manager
- •Set
ANDROID_NDK_HOMEenvironment variable - •Check if dependency actually needs NDK
- •Consider excluding unwanted ABIs in
build.gradle.kts
6. Resource Issues
Issue: Duplicate Resources
code
Error: Duplicate resources
Solution:
- •Check
android/app/src/main/res/for duplicates - •Review resource merging in
build.gradle.kts - •Use resource prefixes to avoid conflicts
- •Exclude duplicates from dependencies
Issue: Missing Resources
code
Error: Resource not found
Solution:
- •Verify resource files exist in correct folders
- •Check resource naming (lowercase, no special chars)
- •Ensure resources are in appropriate density folders
- •Run
flutter pub getto regenerate
7. Plugin Integration Issues
Issue: Plugin Registration Fails
code
Error: Unhandled Exception: MissingPluginException
Solution:
- •Run
flutter clean && flutter pub get - •Check
.flutter-plugins-dependencies - •Verify plugin in
pubspec.yaml - •Rebuild native code:
flutter build apk --verbose
Issue: Plugin Version Conflict
code
Error: Plugin version incompatible
Solution:
- •Check plugin's minimum Flutter/Android versions
- •Update Flutter SDK if needed
- •Update plugin to compatible version
- •Review plugin's changelog for breaking changes
8. Manifest Issues
Issue: Manifest Merge Fails
code
Error: Manifest merger failed
Solution:
- •Check
android/app/src/main/AndroidManifest.xml - •Review plugin manifests causing conflicts
- •Add merge rules if needed:
xml
<uses-sdk tools:overrideLibrary="conflicting.package" />
- •Use
tools:replaceortools:mergeattributes
Issue: Permission Issues
code
Error: Permission denied
Solution:
- •Add required permissions to
AndroidManifest.xml - •Check permission compatibility with minSdkVersion
- •Review runtime permission handling in code
Project-Specific Build Configuration
This Template Uses
- •Java 17: Required for optimal performance
- •Gradle 8.0+: Modern build system
- •Flutter 3.10.1+: Latest stable features
- •Kotlin: For Android native code
- •R8: Code shrinking enabled
- •Parallel builds: Optimized for performance
- •Build cache: Enabled for faster rebuilds
Key Files
- •pubspec.yaml: Flutter dependencies
- •android/build.gradle.kts: Root Gradle config
- •android/app/build.gradle.kts: App-specific Gradle config
- •android/gradle.properties: Local build settings
- •android/gradle-ci.properties: CI-optimized settings
- •android/app/src/main/AndroidManifest.xml: App manifest
Systematic Debug Approach
Level 1: Quick Fixes (Try First)
bash
flutter clean flutter pub get flutter build apk --verbose
Level 2: Gradle Reset
bash
cd android ./gradlew clean ./gradlew --stop cd .. flutter clean flutter pub get flutter build apk --verbose
Level 3: Cache Clear
bash
flutter clean rm -rf ~/.gradle/caches/ rm -rf ~/.pub-cache/ flutter pub get flutter build apk --verbose
Level 4: Deep Dive
bash
# Check Flutter setup flutter doctor -v # Verify Java version java -version # Check Gradle dependencies cd android ./gradlew app:dependencies # Build with stacktrace ./gradlew assembleRelease --stacktrace --info # Or with Flutter verbose flutter build apk --verbose --debug
Build Performance Optimization
If builds work but are slow:
Local Development
- •Use
android/gradle.properties - •Enable parallel builds:
org.gradle.parallel=true - •Increase workers:
org.gradle.workers.max=4 - •More memory:
org.gradle.jvmargs=-Xmx4g - •Enable configuration cache:
org.gradle.configuration-cache=true
CI Environment
- •Use
android/gradle-ci.properties - •Fewer workers:
org.gradle.workers.max=2 - •Less memory:
org.gradle.jvmargs=-Xmx3g - •Disable daemon:
org.gradle.daemon=false - •Disable configuration cache:
org.gradle.configuration-cache=false
Troubleshooting Checklist
- • Run
flutter doctor -v- all checks pass? - • Java version is 17? (
java -version) - • Flutter version matches project requirements?
- • Run
flutter clean && flutter pub get - • Try
cd android && ./gradlew clean - • Check for conflicting dependencies
- • Review
pubspec.yamlfor errors - • Verify
android/app/build.gradle.ktssettings - • Check
AndroidManifest.xmlis valid - • Look for file permission issues
- • Test on different machine if possible
- • Review full error message with
--verbose - • Check GitHub Issues for similar problems
Quick Reference Commands
bash
# Clean build flutter clean && flutter pub get && flutter build apk # Verbose build flutter build apk --verbose # Check dependencies flutter pub outdated cd android && ./gradlew app:dependencies # Gradle info cd android && ./gradlew --version ./gradlew tasks --all # Fix common issues dart fix --apply flutter analyze # Update dependencies flutter pub upgrade # Check Flutter setup flutter doctor -v # Test specific build type flutter build apk --debug flutter build apk --profile flutter build apk --release
When to Seek Help
If after trying all solutions the build still fails:
- •
Document the issue:
- •Full error message
- •Build command used
- •Flutter doctor output
- •Java version
- •Steps already tried
- •
Check resources:
- •Project's TROUBLESHOOTING.md
- •Flutter GitHub Issues
- •Stack Overflow
- •Plugin's GitHub Issues
- •
Create minimal reproduction:
- •Start with
flutter create test_app - •Add dependencies one by one
- •Identify which dependency causes issue
- •Start with
Additional Resources
- •Flutter Build Modes
- •Gradle Performance Guide
- •Android Build Configuration
- •Project docs:
BUILD_OPTIMIZATION.md,TROUBLESHOOTING.md