Search code examples
javaflutterkotlingradle

Gradle Android Version Mismatch in Flutter


so I keep getting this error: Your project's Gradle version is incompatible with the Java │ │ version that Flutter is using for Gradle.

Gradle version is 7.5

Im using this kotlin version ext.kotlin_version = '1.9.23'

tools: classpath 'com.android.tools.build:gradle:7.3.0'

and java

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

since I was facing the problem I deleted my project and cloned my last working commit, all the above snippets are from this codebase.

but since restore Im still facing the issue, so this is my flutter doctor --verbose for more info, since I had upgraaded by android studio to ladybug and then downgraded it back to flamingo during restore.

Moreover, when I build an apk for android from android studio it successfully creates an apk.


Solution

  • I recently deleted my Android Studio (Koala version) and installed Android Studio (Ladybug version). About my Flutter, I also upgraded it, but with command:

    $ flutter upgrade
    

    After these, when I tried to run my older Flutter projects, but errors occurred stating incompatibilities between Gradle and Java versions. To fix this, I had to update version informations in two files, namely:

    1. <project_root>/android/settings.gradle file and,
    2. <project_root>/android/gradle/wrapper/gradle-wrapper.properties .

    Update values used in your older project with the values used in a new app created after upgrading Flutter. Example: corresponding to new settings.gradle file, I updated the older project values from:

    plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "7.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
    }
    

    to:

    plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
    }
    

    and in file gradle-wrapper.properties of the old project, changed the values from:

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-all.zip
    

    to:

    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME
    zipStorePath=wrapper/dists
    distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
    

    and then compiled and ran the project, and this time the errors were fixed and the project ran successfully. You can find the Gradle and Java compatibility information in Compatibility Matrix .

    In your case, I can see you are using OpenJDK version 17, so the appropriate Gradle version form the Compatibility Matrix is 7.3 . So, you may have to update file <project_root>/android/gradle/wrapper/gradle-wrapper.properties to have the appropriate line replaced with:

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