Duplicate Kotlin classes in an Android build after a Kotlin upgrade
The build fails with a duplicate class error naming a kotlin classShort 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.
Fix it fast
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-stdlib2Match what you see, then jump to its fix
- 1
The 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
The 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
- 1
What does the error look like?
The build fails with a duplicate class error naming a kotlin classThe 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:assembleDebugConfirmed by kotlinlang.org
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-stdlibConfirmed by kotlinlang.org
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:
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Nuclear Home VideoA video-streaming Android app built from scratch, engineered for smooth playback.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Mobile apps
Android build fails because a product flavor has no flavor dimension
The flavor 'flavor_name' is not assigned to a flavor dimension.
Android Gradle plugin refuses to build when a product flavor is not assigned to a flavor dimension.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
Android build fails because two libraries ship the same META-INF file
More than one file was found with OS independent path
Two dependencies put a file at the same path inside the APK and the plugin will not guess which one wins.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix