Android “Duplicate class found in modules”: exclude the transitive copy
Find both providers with ./gradlew app:dependencies, then exclude the transitive copy or align versions.
Run ./gradlew :app:dependencies to find which declarations bring in each copy. Then exclude the transitive duplicate: implementation("com.lib:two:2.0") { exclude(group = "com.lib", module = "one") }, or align both on a single version.
Try these fixes in order
Find both providers
Print the runtime dependency tree and locate both artifacts named in the error to see which declarations pull them in.
./gradlew :app:dependencies --configuration debugRuntimeClasspathExclude the transitive duplicate
If one copy arrives transitively, exclude it from the dependency that drags it in, keeping the direct declaration.
Or align the versions
If the duplication is a version skew of the same library family, force a single version through the version catalog or a BOM.
Rebuild and confirm
Clean and rebuild; the duplicate-class error should be gone and the tree should show one provider.
./gradlew :app:assembleDebugInterpret what you see
The error names two modules
What it suggests: The error names both modules providing the class. The dependency tree shows which of your declarations pulls each one in.
Next check: Run ./gradlew :app:dependencies and search for the two artifacts.
One provider looks like a repackaged copy
What it suggests: A common pattern: you declare library A directly while library B bundles the same classes inside its AAR.
Next check: Check whether one provider is a fat AAR you can replace with a modular dependency.
Mixed old and new versions of related libraries
What it suggests: Version catalogs and BOMs usually prevent this; the conflict often comes from a dependency added outside the catalog.
Next check: Align both providers on one version or exclude the transitive one.
Why this happens
- Two dependencies ship the same class, usually because a library is included both directly and transitively.
- A fat AAR bundles classes that another dependency also provides.
Still not working?
Do not enable packagingOptions pickFirsts for the duplicated class as a permanent fix; it hides which copy wins and can mask binary incompatibilities. Resolve the dependency duplication instead.
Official sources
These steps are based on the linked documentation and general diagnostics. BugMedic has not independently tested every product and system configuration. Review commands before running them.