NeuroAgent

Fix Kotlin Serialization Version Incompatibility Error

Resolve the 'Class kotlin.Unit was compiled with an incompatible version of Kotlin' error when using kotlinx-serialization-json 1.9.0. Learn how to fix version mismatches between Kotlin and serialization libraries.

How to resolve the “Class ‘kotlin.Unit’ was compiled with an incompatible version of Kotlin” error when using kotlinx-serialization-json 1.9.0?

I’m encountering a compilation error after adding the kotlinx-serialization-json dependency to my Android project:

kotlin
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")

The error message states:

Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The actual metadata version is 2.2.0, but the compiler version 1.9.0 can read versions up to 2.0.0.
The class is loaded from /Users/user/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.2.0/fdfc65fbc42fda253a26f61dac3c0aca335fae96/kotlin-stdlib-2.2.0.jar!/kotlin/Unit.class

I’ve tried the following troubleshooting steps:

  • Invalidating caches and restarting
  • Updating Gradle and TOML files
  • Running the upgrade assistant
  • Switching to the latest Android Studio (2025.2.1)
  • Deleting the problematic jar file

The error persists, and only goes away when I remove the kotlinx-serialization-json dependency and comment out all serialization references.

Here’s my app build.gradle.kts configuration:

kotlin
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
}

android {
    namespace = "com.sleepfuriously.paulsapp"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.sleepfuriously.paulsapp"
        minSdk = 24
        targetSdk = 34
        versionCode = 4
        versionName = "1.12"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    // ... other dependencies ...
    
    // json serialization
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}

And my libs.versions.toml includes:

toml
[versions]
kotlin = "1.9.0"
// ... other versions ...

How can I resolve this Kotlin version incompatibility issue and successfully use kotlinx-serialization-json in my project?

NeuroAgent

The error occurs because kotlinx-serialization-json 1.9.0 requires Kotlin 2.2.0, while your project is using Kotlin 1.9.0. According to the Kotlin documentation and GitHub releases, version 1.9.0 of kotlinx-serialization requires Kotlin 2.2.0, which introduces metadata version 2.2.0 that your current Kotlin compiler cannot read.


Contents


Understanding the Version Incompatibility

The error occurs due to a metadata version mismatch between your Kotlin compiler and the kotlinx-serialization-json library. According to Stack Overflow discussions, when you use kotlinx-serialization-json 1.9.0, it pulls in dependencies compiled with Kotlin 2.2.0, but your project uses Kotlin 1.9.0 which can only read metadata up to version 2.0.0.

The actual metadata version is 2.2.0, but the compiler version 1.9.0 can read versions up to 2.0.0.

This incompatibility manifests as the “Class ‘kotlin.Unit’ was compiled with an incompatible version of Kotlin” error.


Solution 1: Update Kotlin to 2.2.0

The recommended approach is to update your Kotlin version to 2.2.0 to match the requirements of kotlinx-serialization 1.9.0.

Update your libs.versions.toml:

toml
[versions]
kotlin = "2.2.0"
# Update other versions if needed for Kotlin 2.2.0 compatibility

Update your app build.gradle.kts:

kotlin
android {
    // ... existing configuration ...
    
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    composeOptions {
        // Update Compose compiler version for Kotlin 2.2.0
        kotlinCompilerExtensionVersion = "1.5.8" // or compatible version
    }
}

Add the Kotlin serialization plugin:

kotlin
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    kotlin("plugin.serialization") version "2.2.0" // Add this line
}

This solution ensures compatibility between your Kotlin compiler and the serialization library.


Solution 2: Use Compatible Serialization Version

If you prefer to stay with Kotlin 1.9.0, use an older version of kotlinx-serialization that’s compatible with it.

Update your dependency:

kotlin
dependencies {
    // Use version 1.6.3 instead of 1.9.0
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
}

According to Stack Overflow, 1.6.x versions are compatible with Kotlin 1.9.x:

For the JSON library, 1.6.x versions go with the 1.9.x Kotlin versions (docs). The very latest is 1.6.3, which you might as well use.

This approach allows you to maintain your current Kotlin version while still using serialization functionality.


Solution 3: Verify Plugin Configuration

Ensure all Kotlin-related plugins and dependencies are properly configured and version-aligned.

Check your build.gradle.kts structure:

kotlin
plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    kotlin("plugin.serialization") version "2.2.0" // Ensure version matches Kotlin
}

android {
    // ... existing configuration ...
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
}

Verify your libs.versions.toml:

toml
[versions]
kotlin = "2.2.0"
kotlinx-serialization = "1.9.0"

[libraries]
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinx-serialization" }

[plugins]
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }

This ensures all Kotlin-related components use consistent versions.


Additional Troubleshooting Steps

If the above solutions don’t resolve the issue, try these additional steps:

1. Clean and rebuild your project:

bash
./gradlew clean build --rerun-tasks

2. Check for conflicting dependencies:

kotlin
// In your build.gradle.kts
configurations.all {
    resolutionStrategy {
        force("org.jetbrains.kotlin:kotlin-stdlib:2.2.0")
    }
}

3. Update Gradle wrapper:

Ensure you’re using a compatible Gradle version (8.0 or later) for Kotlin 2.2.0.

4. Clear Gradle cache:

bash
./gradlew --stop
rm -rf ~/.gradle/caches/

5. Check for transitive dependencies:

Use ./gradlew :app:dependencies to see the dependency tree and identify any conflicting versions.


Best Practices for Version Management

Version Compatibility Matrix:

Kotlin Version kotlinx-serialization Version
1.9.x 1.6.x
2.0.x 1.6.x
2.2.x 1.9.x

Regular Updates:

Multi-module Projects:

For complex projects, ensure all modules use consistent Kotlin and serialization versions. As noted in GitHub discussions, version mismatches across modules can cause similar issues.


Sources

  1. Kotlin Documentation - Serialization
  2. GitHub - Kotlinx Serialization Releases
  3. Stack Overflow - Kotlinx Serialization Compatibility
  4. Stack Overflow - Version Compatibility Guide
  5. GitHub - Kotlinx Serialization Changelog

Conclusion

To resolve the “Class ‘kotlin.Unit’ was compiled with an incompatible version of Kotlin” error:

  1. Update to Kotlin 2.2.0 - This is the recommended solution as it fully supports kotlinx-serialization 1.9.0
  2. Use kotlinx-serialization 1.6.3 - If you prefer to stay with Kotlin 1.9.x
  3. Ensure plugin consistency - Verify all Kotlin-related plugins use compatible versions
  4. Clean and rebuild - After making version changes, perform a clean rebuild
  5. Check dependency conflicts - Use Gradle dependency tree analysis to identify version mismatches

The key takeaway is maintaining version compatibility across all Kotlin-related dependencies and plugins. Starting with Kotlin 2.2.0 and kotlinx-serialization 1.9.0 provides the most current and feature-rich solution, while using the older serialization version offers a stable path if you’re not ready to upgrade your Kotlin version.