Skip to content

Module Compiled with Incompatible Kotlin Version

The Problem

When working with Android projects using Kotlin, you may encounter the error:

Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is X.X.X, expected version is X.X.X

This error occurs when there's a mismatch between:

  • The Kotlin version used to compile a dependency
  • The Kotlin version your project expects

The error typically manifests after updating dependencies, Android Studio, or Gradle plugins, creating version incompatibilities.

Root Cause

The core issue stems from version mismatches between three key components:

  1. Kotlin Gradle Plugin (KGP) version in your project
  2. Android Gradle Plugin (AGP) version
  3. Kotlin version used by external dependencies

These components must be compatible with each other for successful compilation.

Solutions

For newer Flutter/Android projects (2024+), the primary fix is updating the Kotlin version in your android/settings.gradle file:

kotlin
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.1" apply false
    id "org.jetbrains.kotlin.android" version "2.0.20" apply false // Update this line
}

TIP

Always check the Kotlin releases page for the latest version.

2. Update Project-Level build.gradle

For older projects, you may need to update the android/build.gradle file:

kotlin
buildscript {
    ext.kotlin_version = '1.9.22' // Update to match your needs
    
    repositories {
        google()
        jcenter()
    }

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

3. Ensure Version Compatibility

Refer to the official compatibility table to match Kotlin with AGP versions:

AGP VersionKotlin Version
8.3.x1.9.22
8.1.x1.9.0+
8.0.x1.8.20+
7.4.x1.8.0+
7.3.x1.7.20+

Check the official compatibility guide for current requirements.

4. Clean Gradle Caches

Sometimes outdated cached dependencies cause this issue. Clean them with:

bash
# macOS/Linux
rm -r $HOME/.gradle/caches/

# Windows
rmdir /s %USERPROFILE%\.gradle\caches

Alternatively, use Android Studio's cache invalidation:

  • File → Invalidate Caches / Restart

5. Update Gradle Wrapper

Ensure your gradle-wrapper.properties uses a compatible Gradle version:

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

6. Check Dependency Conflicts

Some third-party libraries might require specific Kotlin versions. If you recently added a dependency and encountered this error:

  1. Check if the library requires a specific Kotlin version
  2. Either update your Kotlin version to match
  3. Or downgrade the library to a compatible version

WARNING

Avoid using version ranges (2.+) in dependencies as they can unexpectedly pull incompatible versions.

7. Complete Fix Example (2024)

For a comprehensive solution ensuring all components are compatible:

kotlin
buildscript {
    ext.kotlin_version = '1.9.22'
    ext {
        compose_compiler = '1.5.10'
        compose_version = '1.6.3'
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
kotlin
plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.3.0" apply false
    id "org.jetbrains.kotlin.android" version "1.9.22" apply false
}
properties
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
kotlin
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    
    kotlinOptions {
        jvmTarget = '17'
    }
}

Prevention

To avoid future compatibility issues:

  1. Regularly update: Keep Android Studio, AGP, and Kotlin versions current
  2. Check compatibility: Before updating任何 component, verify version compatibility
  3. Avoid version ranges: Use specific version numbers in dependencies
  4. Monitor dependencies: Be aware when adding new libraries that they might require Kotlin version updates

INFO

Android Studio's AGP Upgrade Assistant (Tools → AGP Upgrade Assistant) can help automate compatibility checks.

When All Else Fails

If you've tried all solutions and still face issues:

  1. Check memory settings: Increase Gradle memory in gradle.properties:

    properties
    org.gradle.jvmargs=-Xmx2048m
  2. Create a new project: Compare configuration files with a newly created project

  3. Seek help: Share your complete configuration files on developer forums, as the issue might be specific to your setup

Remember that Kotlin and Android tooling evolves rapidly, so staying updated with current compatibility requirements is key to avoiding these compilation issues.