Fix Kotlin Android Duplicate Class Errors
Problem Statement
When building Android projects with Kotlin, you may encounter duplicate class errors like:
Duplicate class kotlin.collections.jdk8.CollectionsJDK8Kt found in modules jetified-kotlin-stdlib-1.8.0 and jetified-kotlin-stdlib-jdk8-1.6.0
This occurs when multiple versions of Kotlin standard library dependencies are included in your build. The conflict arises because:
kotlin-stdlib-jdk7
andkotlin-stdlib-jdk8
were merged intokotlin-stdlib
starting in Kotlin 1.8- Dependencies are including different Kotlin versions (e.g., 1.8.0 in one dependency vs 1.6.0 in another)
- Manual deletion of libraries won't fix it as Gradle redownloads dependencies on rebuild
The common error patterns are:
- Classes existing in both
kotlin-stdlib
andkotlin-stdlib-jdkX
- Version mismatches between Kotlin dependencies
- Conflicts with other libraries like the Android Annotation library
Recommended Solutions
1. Update Kotlin Gradle Plugin (Most Common Fix)
Update Kotlin dependencies to version 1.8.0+ in both project and app-level build files.
plugins {
id 'com.android.application' version '8.0.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
android {
// ...
}
dependencies {
// Remove explicit stdlib-jdk if present
// implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:..." // ❌ REMOVE
// implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:..." // ❌ REMOVE
}
2. Add Kotlin Bom for Dependency Alignment
Align Kotlin versions using the BOM (Bill of Materials):
dependencies {
// Add this before other dependencies
implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
// Other dependencies...
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
}
3. Add Dependency Constraints (For Legacy Projects)
Force dependency versions where conflicts persist:
dependencies {
constraints {
// Align these dependencies with your current Kotlin version
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") {
because("Merged into kotlin-stdlib in 1.8.0")
}
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
because("Merged into kotlin-stdlib in 1.8.0")
}
}
}
4. Troubleshooting Conflicts
If errors persist:
- Check conflicting libraries:
./gradlew :app:dependencies
Use Android Studio's dependency analyzer:
Tools → Android → SDK Manager → SDK Tools → Android SDK Build-ToolsVerify compatibility with other libraries:
composeOptions {
kotlinCompilerExtensionVersion = "1.4.7" // Must match Kotlin plugin
}
Additional Solutions for Specific Cases
Case: Firebase Conflicts
implementation platform('com.google.firebase:firebase-bom:32.2.0') // Update to latest
Case: Annotation Library Conflicts
implementation 'androidx.annotation:annotation:1.6.0' // Use current stable version
Case: Clean Cache After Changes
After modifying build files:
- In Android Studio: File → Invalidate Caches / Restart
- Run command:
./gradlew clean build
How Verison Alignement Works
Starting Kotlin 1.8:
stdlib-jdk7
andstdlib-jdk8
became subsets ofstdlib
- Using different versions causes duplicate classes
- The error shows conflicting paths:
Duplicate class ... found in jetified-kotlin-stdlib-1.8.0 (new) and jetified-kotlin-stdlib-jdk7-1.6.0 (old)
The solutions ensure:
- All Kotlin-related dependencies use the same version
- Legacy jdk7/jdk8 artifacts get aligned with main stdlib
- Dependency resolution doesn't pull multiple versions