Android build stops with Duplicate class ... found in modules after a new dependency
Fix it fast
Most common cause, 1 of 3A jar or aar in libs/ duplicates a library that also comes from Maven
Keep one of the two.
// 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")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.
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.
./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:
- 1A jar or aar in libs/ duplicates a library that also comes from Maven
One 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 - 2A library already bundles another library that you also declare
The duplicated class belongs to library B, but one module in the message is library A, whose own binary contains B's classes - 3One library reaches the build under two different coordinates
The 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-errorsProgram 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.
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.
// 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.
./gradlew :app:checkDebugDuplicateClasses :app:assembleDebugConfirmed by developer.android.com
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.
./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.
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.
./gradlew :app:checkDebugDuplicateClassesConfirmed by developer.android.com
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.
grep -n -E " asm:asm:|org\.ow2\.asm:asm:" deps.txtFix
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.
// app/build.gradle.kts
configurations.configureEach {
exclude(group = "asm", module = "asm")
}Verify
The dependency report shows only the new coordinates, and checkDebugDuplicateClasses passes.
./gradlew :app:dependencies --configuration debugRuntimeClasspath | grep -E " asm:asm:|org\.ow2\.asm:asm:"
./gradlew :app:checkDebugDuplicateClassesConfirmed by docs.gradle.org
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.