Skip to content

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:

  1. kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 were merged into kotlin-stdlib starting in Kotlin 1.8
  2. Dependencies are including different Kotlin versions (e.g., 1.8.0 in one dependency vs 1.6.0 in another)
  3. 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 and kotlin-stdlib-jdkX
  • Version mismatches between Kotlin dependencies
  • Conflicts with other libraries like the Android Annotation library

1. Update Kotlin Gradle Plugin (Most Common Fix)

Update Kotlin dependencies to version 1.8.0+ in both project and app-level build files.

gradle
plugins {
    id 'com.android.application' version '8.0.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
gradle
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):

gradle
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'
}
<tip> The BOM ensures consistent versions across all Kotlin libraries. </tip>

3. Add Dependency Constraints (For Legacy Projects)

Force dependency versions where conflicts persist:

gradle
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:

  1. Check conflicting libraries:
gradle
./gradlew :app:dependencies
  1. Use Android Studio's dependency analyzer:
    Tools → Android → SDK Manager → SDK Tools → Android SDK Build-Tools

  2. Verify compatibility with other libraries:

gradle
composeOptions {
    kotlinCompilerExtensionVersion = "1.4.7" // Must match Kotlin plugin
}
<warning> Avoid using open-ended version specifiers like `core-ktx:+`, which can cause unexpected version upgrades. </warning>

Additional Solutions for Specific Cases

Case: Firebase Conflicts

gradle
implementation platform('com.google.firebase:firebase-bom:32.2.0') // Update to latest

Case: Annotation Library Conflicts

gradle
implementation 'androidx.annotation:annotation:1.6.0' // Use current stable version

Case: Clean Cache After Changes

After modifying build files:

  1. In Android Studio: File → Invalidate Caches / Restart
  2. Run command:
shell
./gradlew clean build

How Verison Alignement Works

Starting Kotlin 1.8:

  • stdlib-jdk7 and stdlib-jdk8 became subsets of stdlib
  • 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:

  1. All Kotlin-related dependencies use the same version
  2. Legacy jdk7/jdk8 artifacts get aligned with main stdlib
  3. Dependency resolution doesn't pull multiple versions
<tip> In Android Studio Flamingo (2023.3+) and newer versions, Kotlin is automatically installed - verify versions in Analyze → Dump View → Dependency Graph. </tip>