Duplicate class in Kotlin Android: Top 5 Errors and Solutions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The "Double" in Your Project

\n\n

Imagine 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\n

In 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\n

What is Duplicate class and why does it happen?

\n\n

The 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.

\n\n

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\n

Why 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.

\n\n

Top 5 Causes of the Duplicate class Error in Kotlin Android

\n\n

We've gathered the most common scenarios you'll encounter in real-world development. Each point includes a code example and a solution.

\n\n

1. Kotlin stdlib Version Conflict

\n\n

This 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.

\n\n

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\n

Gradle will output something like:

\n
Duplicate 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\n

Solution: Use a single Kotlin version for the entire project. The best way is to set it in the root build.gradle.kts:

\n
// 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\n

You can also explicitly exclude the dependency:

\n
implementation("com.some.library:example:2.0.0") {\n    exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib")\n}\n
\n\n

2. Duplication of AndroidX and Support Library

\n\n

A 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).

\n\n

Example error:

\n
Duplicate class android.support.v4.app.Fragment found in modules support-compat-28.0.0 and fragment-1.6.0\n
\n\n

Solution:

\n
    \n
  • Enable the flag android.useAndroidX=true in gradle.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

Blogs

Book Recommendations