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.
Recommended Solutions
Method 1: Update Gradle Manually via Command Line
The most straightforward approach is using the Gradle wrapper command:
cd android
./gradlew wrapper --gradle-version=7.6.1This 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:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip2. Update android/build.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:
android {
compileSdkVersion 33
defaultConfig {
minSdkVersion 21
targetSdkVersion 33
// ... other configurations
}
// ... other configurations
}Method 3: Use Android Studio's Built-in Upgrade Tool
- Right-click the
androidfolder in your Flutter project - Select Flutter → Open Android module in Android Studio
- Wait for the project to load completely
- Android Studio will typically detect outdated Gradle versions and suggest upgrades
- Follow the prompts to update both the Android Gradle Plugin and Gradle versions
- Run
flutter cleanafter 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:
- Backup your
androidfolder (especiallykey.properties) - Delete the
androidfolder - Run from your project root:bash
flutter create . - Restore any custom configurations from your backup
Version Compatibility
Ensure compatibility between these components:
| Component | Recommended Version |
|---|---|
| Gradle | 7.5-7.6.1 |
| Android Gradle Plugin | 7.3.1-7.4.2 |
| Kotlin | 1.7.10+ |
| Java | 11 or 17 |
INFO
Check the Gradle compatibility matrix to ensure your Java version matches the Gradle requirements.
Troubleshooting Tips
- After updating Gradle, run
flutter cleanto clear build artifacts - Use
flutter pub cache cleanif dependency issues persist - Verify your Java version matches Gradle requirements with
java -version - Check plugin documentation for specific version requirements
Common Pitfalls
- Mismatched versions: Ensure Android Gradle Plugin, Gradle, and Kotlin versions are compatible
- Cached dependencies: Clean build caches after version changes
- Plugin incompatibility: Some older plugins may not support the latest Gradle versions
- 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.