Introduction: The "Double" in Your Project
\n\nImagine this: you're writing Kotlin code for Android, everything works perfectly. You add a new library, run the build — and Gradle throws a red error: "Duplicate class". Sound familiar? If you've ever worked with Android development, you've likely seen this message. On Stack Overflow, this question has garnered thousands of views, and for good reason — the error can frustrate even experienced developers.
\n\nIn this article, we'll break down what Duplicate class in Kotlin Android is, why it occurs, and most importantly — how to fix it once and for all. You'll learn the 5 most common causes, get ready-made solutions, and learn how to prevent dependency conflicts. Let's go!
\n\nWhat is Duplicate class and why does it happen?
\n\nThe Duplicate class error occurs when the build system (Gradle) finds two or more identical classes with the same fully qualified name (FQN). In the Android world, this usually means the same class exists in different libraries or modules of your project.
Imagine you ordered two identical books from different publishers. They sit on the same shelf — and the librarian doesn't know which one to take. That's Gradle: it can't decide which duplicate class to use.
\n\nWhy is this especially relevant for Kotlin? Kotlin actively uses the Kotlin standard library (kotlin-stdlib), which can conflict with its own versions from other sources. Add AndroidX, Google libraries, Firebase — and you get a volatile mix.
Top 5 Causes of the Duplicate class Error in Kotlin Android
\n\nWe've gathered the most common scenarios you'll encounter in real-world development. Each point includes a code example and a solution.
\n\n1. Kotlin stdlib Version Conflict
\n\nThis is the king of all duplicates. When you add a library that pulls its own version of kotlin-stdlib, and your project already has a different one — a conflict arises.
Example:
\n// build.gradle.kts (app module)\ndependencies {\n implementation("org.jetbrains.kotlin:kotlin-stdlib:1.9.0")\n implementation("com.some.library:example:2.0.0") // this library pulls kotlin-stdlib:1.8.0\n}\n\n\nGradle will output something like:
\nDuplicate class kotlin.collections.ArraysKt found in modules kotlin-stdlib-1.9.0 (org.jetbrains.kotlin:kotlin-stdlib:1.9.0) and kotlin-stdlib-1.8.0 (org.jetbrains.kotlin:kotlin-stdlib:1.8.0)\n\n\nSolution: Use a single Kotlin version for the entire project. The best way is to set it in the root build.gradle.kts:
// build.gradle.kts (root)\nplugins {\n kotlin("android") version "1.9.0" apply false\n}\n\n// build.gradle.kts (app module)\nplugins {\n kotlin("android")\n}\n\n\nYou can also explicitly exclude the dependency:
\nimplementation("com.some.library:example:2.0.0") {\n exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")\n}\n\n\n2. Duplication of AndroidX and Support Library
\n\nA classic scenario. If you use old Support Library along with AndroidX, they contain identical classes (e.g., android.support.v4.app.Fragment and androidx.fragment.app.Fragment).
Example error:
\nDuplicate class android.support.v4.app.Fragment found in modules support-compat-28.0.0 and fragment-1.6.0\n\n\nSolution:
\n- \n
- Enable the flag
android.useAndroidX=trueingradle.properties. \n - Ensure all libraries are migrated to AndroidX. Use the migration tool in Android Studio (Refactor > Migrate to AndroidX). \n
- If a library does not support AndroidX, use