Skip to content

Gradle Version Incompatibility in Flutter Projects

When building Flutter Android applications, you might encounter Gradle version compatibility issues that prevent successful builds. The error typically occurs when third-party plugins require a newer Gradle version than what your project is configured to use.

Problem Statement

The common error message appears as:

FAILURE: Build failed with an exception.

* Where:
Build file '.../android/build.gradle' line: 25

* What went wrong:
A problem occurred evaluating root project 'audioplayers'.
> Failed to apply plugin [id 'kotlin-android']
   > The current Gradle version 4.10.2 is not compatible with the Kotlin Gradle plugin. 
   > Please use Gradle 5.3 or newer, or the previous version of the Kotlin plugin.

This indicates that your Flutter project's Gradle configuration is outdated and incompatible with modern Android development requirements, particularly when using Kotlin-based plugins.

Method 1: Update Gradle Manually via Command Line

The most straightforward approach is using the Gradle wrapper command:

bash
cd android
./gradlew wrapper --gradle-version=7.6.1

This updates the distributionUrl in android/gradle/wrapper/gradle-wrapper.properties automatically.

TIP

Replace 7.6.1 with the version recommended in the Flutter Gradle migration guide

Method 2: Update Configuration Files Manually

If you prefer manual updates, modify these files:

1. Update android/gradle/wrapper/gradle-wrapper.properties:

properties
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip

2. Update android/build.gradle:

gradle
buildscript {
    ext.kotlin_version = '1.7.10' // Update Kotlin version if needed
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2' // Updated AGP version
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

3. Update android/app/build.gradle:

gradle
android {
    compileSdkVersion 33
    
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        // ... other configurations
    }
    // ... other configurations
}

Method 3: Use Android Studio's Built-in Upgrade Tool

  1. Right-click the android folder in your Flutter project
  2. Select FlutterOpen Android module in Android Studio
  3. Wait for the project to load completely
  4. Android Studio will typically detect outdated Gradle versions and suggest upgrades
  5. Follow the prompts to update both the Android Gradle Plugin and Gradle versions
  6. Run flutter clean after the upgrade

WARNING

Always check the Flutter documentation for compatible version combinations before updating.

Method 4: Recreate Android Folder (Last Resort)

If your Android project doesn't have extensive customizations:

  1. Backup your android folder (especially key.properties)
  2. Delete the android folder
  3. Run from your project root:
    bash
    flutter create .
  4. Restore any custom configurations from your backup

Version Compatibility

Ensure compatibility between these components:

ComponentRecommended Version
Gradle7.5-7.6.1
Android Gradle Plugin7.3.1-7.4.2
Kotlin1.7.10+
Java11 or 17

INFO

Check the Gradle compatibility matrix to ensure your Java version matches the Gradle requirements.

Troubleshooting Tips

  • After updating Gradle, run flutter clean to clear build artifacts
  • Use flutter pub cache clean if dependency issues persist
  • Verify your Java version matches Gradle requirements with java -version
  • Check plugin documentation for specific version requirements

Common Pitfalls

  1. Mismatched versions: Ensure Android Gradle Plugin, Gradle, and Kotlin versions are compatible
  2. Cached dependencies: Clean build caches after version changes
  3. Plugin incompatibility: Some older plugins may not support the latest Gradle versions
  4. Java version conflicts: Verify your Java installation matches Gradle's requirements

By following these methods, you should resolve Gradle compatibility issues and successfully build your Flutter Android applications.