Incompatible Classes Were Found in Dependencies. Remove Them From the Classpath

When developing in Flutter you might encounter this error: “Incompatible classes were found in dependencies. Remove them from the classpath or use ‘-Xskip-metadata-version-check’ to suppress errors“. In this post, we will break down the error and go over the solution to resolve it.

Understand the Incompatible Classes Were Found Error

At the bottom of the error stack, there is a helpful “Flutter Fix” message telling us to update our Kotlin Gradle plugin version in the android\build.gradle file. This outdated plugin version is the cause of the error. The message also provides us a link to the Kotlin releases page where all the available versions are listed.

Resolve the Incompatible Classes Were Found Error

If we go to the link from the error message. We will end up on the Kotlin website and get an overview of all the releases.

Kotlin website list of releases

At the time of writing 2.0.0 is the latest version.

Before Flutter 3.16

As mentioned before the error message also specified the file we need to change: android/build.gradle. Now, if we take a look inside that file, we will find a section called buildscript. Within this section, there is a parameter called ext.kotlin_version that is currently set to a specific version, in my case 1.7.10.

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

To resolve the error we need to change this parameter to either the latest version or the version given in the error message itself. In the error message you can find the following line:

The binary version of its metadata is 1.9.0, expected version is 1.7.1.

Which means that we can replace the old version 1.7.10 with 1.9.0.

buildscript {
    ext.kotlin_version = '1.9.0'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Once you have updated the version, and run your project again you will see that the build is successful.

After Flutter 3.16

For Flutter version 3.16 and higher this is different because the imperative apply of Flutter’s Gradle plugins is deprecated. Not only that, but also the suggested file from the error message is no longer the correct file. Instead we want to make changes inside the android\settings.gradle file.

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
}

Inside the settings.gradle file we want to change the version of org.jetbrains.kotlin.android to either the latest version or the version given in the error message itself. In the error message you can find the following line:

The binary version of its metadata is 1.9.0, expected version is 1.7.1.

Which means that we can replace the old version 1.7.10 with 1.9.0.

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.9.0" apply false
}

Once you have updated the version, and run your project again you will see that the build is successful.

Conclusion

We have seen that if you go through the error stack carefully you can see that the solution was already mentioned. However, in this post, we went over the exact steps to make sure that you apply the solution in the right way. Now whenever you come across the incompatible classes were found in dependencies error again you know how to handle it.

Tijn van den Eijnde
Tijn van den Eijnde
Articles: 40

Leave a Reply

Your email address will not be published. Required fields are marked *