Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Duplicate Kotlin classes in an Android build after a Kotlin upgrade

ErrorThe build fails with a duplicate class error naming a kotlin class

Short answer

Kotlin 1.8 merged kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 into kotlin-stdlib. If an old jdk7 or jdk8 artifact stays on the classpath next to a newer stdlib, the same classes arrive twice. Remove the explicit jdk7 and jdk8 lines and align every stdlib artifact on one version.

  • Applies to Kotlin 1.8 and later, Android Gradle Plugin 8.x, Gradle wrapper
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List which stdlib artifacts and versions Gradle resolves for the app, so you can see the mix.

    ./gradlew :app:dependencies | grep kotlin-stdlib
  2. 2Match what you see, then jump to its fix

    1. 1The listing shows kotlin-stdlib-jdk7 or kotlin-stdlib-jdk8 next to kotlin-stdlib, and your build file names them.kotlin-stdlib-jdk7 or kotlin-stdlib-jdk8 is still declared explicitlyFix
    2. 2The dependency listing shows more than one version of kotlin-stdlib family artifacts, often pulled in by libraries built with an older Kotlin.Different Kotlin stdlib artifact versions meet on the classpathFix

What does the error look like?

  • The build fails with a duplicate class error naming a kotlin class
  • The class is reported in both kotlin-stdlib and kotlin-stdlib-jdk7 or kotlin-stdlib-jdk8

Cause 1 of 2

kotlin-stdlib-jdk7 or kotlin-stdlib-jdk8 is still declared explicitly

1Check

Search the build files and version catalog for the two old artifact names.

grep -rn "kotlin-stdlib-jdk" --include=*.gradle --include=*.kts --include=*.toml .

2Fix

Kotlin's own release notes say the jdk7 and jdk8 contents are now inside kotlin-stdlib. Replace both with kotlin-stdlib.

dependencies {
    // remove these
    // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20")
    // implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20")
    implementation("org.jetbrains.kotlin:kotlin-stdlib:<your Kotlin version>")
}

3Verify

Build again; the duplicate class message should be gone.

./gradlew :app:assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

Cause 2 of 2

Different Kotlin stdlib artifact versions meet on the classpath

1Check

Look at the same dependency listing for version numbers on each kotlin-stdlib line.

./gradlew :app:dependencies | grep "org.jetbrains.kotlin"

2Fix

Kotlin's docs say mixing stdlib artifact versions can cause class duplication. Declare a platform dependency on the Kotlin BOM so all of them use one version.

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom:<your Kotlin version>"))
}

3Verify

Run the listing again and confirm one version across the family.

./gradlew :app:dependencies | grep kotlin-stdlib

Confirmed by kotlinlang.org

Did this fix it?

How do I stop it coming back?

  • Do not declare kotlin-stdlib-jdk7 or jdk8 in new code; use kotlin-stdlib.
  • Upgrade the Kotlin Gradle plugin and Kotlin-based libraries together.
  • Keep Kotlin versions in one place, such as the version catalog.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.