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.
Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules
kotlin-stdlib-1.9.25 and kotlin-stdlib-jdk8-1.6.21Common 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
- Run
./gradlew :app:dependenciesto find who pulls the old split artifact. - Exclude the legacy jdk7/jdk8 stdlib, or force a single stdlib version.
- Re-run the failing task to confirm the duplicate is gone.
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.
dependencies {
implementation(platform("org.jetbrains.kotlin:kotlin-bom:2.0.21"))
}How to prevent it
- Rely on the merged
kotlin-stdliband exclude legacy jdk7/jdk8 parts. - Use the Kotlin BOM or a version catalog to keep one version everywhere.
- Audit
dependenciesoutput when adding libraries that bundle Kotlin.