Skip to content
Latchkey

Kotlin build "Duplicate class ... found in modules" in CI

A "Duplicate class" error means the same fully qualified class is present in two artifacts on the runtime or merge classpath. With Kotlin this commonly happens when old stdlib split modules (kotlin-stdlib-jdk7/-jdk8) collide with a newer merged stdlib, or two library versions overlap.

What this error means

The build fails (during a merge or check task) with "Duplicate class kotlin.collections.X found in modules kotlin-stdlib-1.8 and kotlin-stdlib-jdk8-1.6" or similar.

Gradle
Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules
kotlin-stdlib-1.9.25 and kotlin-stdlib-jdk8-1.6.21

Common causes

Old stdlib split artifacts on the classpath

Since Kotlin 1.8 the jdk7/jdk8 stdlib parts were merged into kotlin-stdlib. A transitive dependency still pulling the old split artifact duplicates classes.

Two versions of the same library resolved

Different modules request different versions of a library and both land on the classpath, duplicating classes.

How to fix it

Exclude or align the stdlib split modules

  1. Run ./gradlew :app:dependencies to find who pulls the old split artifact.
  2. Exclude the legacy jdk7/jdk8 stdlib, or force a single stdlib version.
  3. Re-run the failing task to confirm the duplicate is gone.
build.gradle.kts
configurations.all {
    resolutionStrategy {
        force("org.jetbrains.kotlin:kotlin-stdlib:2.0.21")
    }
    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk7")
    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
}

Align versions with a platform/BOM

Use the Kotlin BOM (or a version catalog) so every Kotlin artifact resolves to one version.

build.gradle.kts
dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.0.21"))
}

How to prevent it

  • Rely on the merged kotlin-stdlib and exclude legacy jdk7/jdk8 parts.
  • Use the Kotlin BOM or a version catalog to keep one version everywhere.
  • Audit dependencies output when adding libraries that bundle Kotlin.

Frequently asked questions

What causes ""Duplicate class ... found in modules""?
Since Kotlin 1.8 the jdk7/jdk8 stdlib parts were merged into kotlin-stdlib. A transitive dependency still pulling the old split artifact duplicates classes.
How do I fix "Duplicate class ... found in modules"?
Exclude or align the stdlib split modules

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card