Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Flutter and app store releases

Android build stops with Duplicate class ... found in modules after a new dependency

Your error

Execution failed for task ':app:checkDebugDuplicateClasses'.3 more lines it can show

Fix it fast

Most common cause, 1 of 3

A jar or aar in libs/ duplicates a library that also comes from Maven

Keep one of the two.

Fix
// app/build.gradle.kts: remove the local copy
// implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("com.example:sdk:2.4.0")
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Duplicate class X found in modules A and B comes from the Android Gradle plugin's checkDebugDuplicateClasses task: the same class reached the runtime classpath from two artifacts. Usually a jar in libs/ duplicates a Maven dependency, a library bundles one you also declare, or one library has two group names. Remove or exclude one copy. AGP 8.x to 9.5.2.

  • Applies to Android Gradle plugin 8.x to 9.5.2, Gradle 8.14 to 9.9.2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Copy the two module names from the first Duplicate class line, then ask Gradle why each one is on the runtime classpath. A path under libs/ points to the first cause, a library pulled in only through another library to the second, and two different group names for one library to the third.

Run this first
./gradlew :app:dependencies --configuration debugRuntimeClasspath > deps.txt
./gradlew :app:dependencyInsight --configuration debugRuntimeClasspath --dependency <name-from-the-error>

Then find the line you see in this list and open its fix:

  1. 1A jar or aar in libs/ duplicates a library that also comes from MavenOne of the two modules in the message is a local file (from app/libs or a files(...) line), the other a group:name:version artifact
  2. 2A library already bundles another library that you also declareThe duplicated class belongs to library B, but one module in the message is library A, whose own binary contains B's classes
  3. 3One library reaches the build under two different coordinatesThe two modules have different group or artifact names but the same library and class names, for example asm:asm and org.ow2.asm:asm

What does the error look like?

  • Execution failed for task ':app:checkDebugDuplicateClasses'.
  • Duplicate class com.example.Foo found in modules ... and ...
  • Learn how to fix dependency resolution errors at https://d.android.com/r/tools/classpath-sync-errors
  • Program type already present com.example.MyClass

Cause 1 of 3

A jar or aar in libs/ duplicates a library that also comes from Maven

You seeOne of the two modules in the message is a local file (from app/libs or a files(...) line), the other a group:name:version artifact

Check

Duplicate class errors with one local module mean the app has a local binary dependency and a remote binary dependency on the same library. This is common in older projects that once copied an SDK jar into libs/ and later added the same SDK through Gradle, or where a fileTree line picks up every jar in the folder.

Check
ls app/libs
grep -n "fileTree\|files(" app/build.gradle*

Fix

Keep one of the two. Prefer the Maven artifact, which carries version metadata, then delete the jar and the line that adds it.

Fix
// app/build.gradle.kts: remove the local copy
// implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("com.example:sdk:2.4.0")

Verify

checkDebugDuplicateClasses passes and the debug APK builds.

Verify
./gradlew :app:checkDebugDuplicateClasses :app:assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

A library already bundles another library that you also declare

You seeThe duplicated class belongs to library B, but one module in the message is library A, whose own binary contains B's classes

Check

Some SDKs ship as a single artifact that includes a copy of another library inside its binary. Declaring that inner library directly as well puts its classes on the classpath twice, once from each artifact, and the plugin's duplicate check fails the build.

Check
./gradlew :app:dependencyInsight --configuration debugRuntimeClasspath --dependency <library-b>

Fix

Remove library B as a direct dependency and use the copy that library A carries. Excluding a transitive dependency of A does not help, since the copied classes are packed inside A's own file.

Fix
dependencies {
    implementation("com.example:library-a:3.1.0")
    // implementation("com.example:library-b:1.8.0")   // already inside library-a
}

Verify

The dependency report lists library B no more than once, and the build passes.

Verify
./gradlew :app:checkDebugDuplicateClasses

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

One library reaches the build under two different coordinates

You seeThe two modules have different group or artifact names but the same library and class names, for example asm:asm and org.ow2.asm:asm

Check

When a library moves to new coordinates, Gradle sees the old and the new artifact as unrelated modules, so conflict resolution keeps both and the duplicate check catches the result. Gradle's documentation uses ASM, which moved from asm:asm to org.ow2.asm:asm, as its example: both on one classpath is illegal because they provide the same feature.

Check
grep -n -E " asm:asm:|org\.ow2\.asm:asm:" deps.txt

Fix

Exclude the old coordinates from every configuration. For a long-term rule, declare that the old module provides the new capability and let Gradle pick the highest version, as the capabilities guide shows.

Fix
// app/build.gradle.kts
configurations.configureEach {
    exclude(group = "asm", module = "asm")
}

Verify

The dependency report shows only the new coordinates, and checkDebugDuplicateClasses passes.

Verify
./gradlew :app:dependencies --configuration debugRuntimeClasspath | grep -E " asm:asm:|org\.ow2\.asm:asm:"
./gradlew :app:checkDebugDuplicateClasses

Confirmed by docs.gradle.org

Did this fix it?

How do I stop it coming back?

  • Keep app/libs empty when a Maven artifact exists, and list any jar that has to stay there by name instead of with a fileTree wildcard.
  • Read an SDK's install guide for bundled libraries before adding its dependencies one by one.
  • Run ./gradlew :app:checkDebugDuplicateClasses in CI on dependency update pull requests, so a duplicate shows up before merge.

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 on the same stack: QistLock, Nuclear Home Video.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.