When building an app in Android Studio,
Duplicate class ... found in modules ...
may be displayed, preventing the app from being built.
This error occurs when the same class is included in multiple libraries or dependencies,
making it impossible for the Android build system to determine which class to use.
This article explains the main causes of
Duplicate class found in modules,
and step by step how to check duplicate dependencies in Android Studio
and resolve the error.
- What Is Duplicate class found in modules?
- Typical Error Message
- First, Check the Duplicate Libraries in the Error Message
- Cause 1: The Same Library Has Been Added Twice
- Cause 2: Different Versions of the Same Library Are Included
- Check Gradle Dependencies
- Check a Specific Dependency in Detail
- Solution 1: Remove Unnecessary Dependencies
- Solution 2: Exclude Duplicate Dependencies with exclude
- Solution 3: Align Library Versions
- Cause 3: Old Support Library and AndroidX Are Mixed
- Cause 4: JAR or AAR Files Have Been Added Manually
- Check the libs Folder
- Cause 5: A Local Module and an External Library Are Duplicated
- Cause 6: Firebase or Google Play services Dependencies Are Inconsistent
- Cause 7: An Old Library Contains Another Library Internally
- Check Whether You Created the Same Class Yourself
- Run Gradle Sync After Fixing Dependencies
- Also Check Clean Project and Rebuild Project
- If Duplicate class found in modules Still Cannot Be Resolved
- When a Large Number of Duplicate class Errors Are Displayed
- Summary
What Is Duplicate class found in modules?
Duplicate class found in modules
is an error that occurs when a class with the same name is found in multiple modules or libraries
when building an app in Android Studio.
In Android apps,
various libraries can be added to a project using Gradle.
However, if multiple added libraries contain the same class,
or if different versions of the same library are loaded at the same time,
duplicate classes may occur.
In this state,
the build process cannot determine which class to use,
so the build stops.
Point:
Duplicate class found in modules
can occur not only when the same class is written twice in Java or Kotlin source code,
but also when Gradle dependencies are duplicated.
In practice, dependencies between libraries are often the cause.
Typical Error Message
Duplicate class com.example.SomeClass found in modules
library-a.jar and library-b.jar
In an actual error,
the names of duplicate libraries,
JARs, AARs, and other modules are displayed after
modules.
For example, as shown below,
you may be able to see that the same class is being loaded from two dependencies.
Duplicate class com.example.SomeClass found in modules
library-one-1.0.jar
and
library-two-2.0.jar
In this case,
check the two modules shown in the error message
and investigate why both have been added to the project.
First, Check the Duplicate Libraries in the Error Message
When
Duplicate class found in modules
occurs, first check the full error message displayed in Build Output.
The library names displayed after
found in modules
are particularly important.
Duplicate class ...
found in modules
library-a
and
library-b
These two are the dependencies that contain the same class.
Many
Duplicate class
errors may be displayed consecutively,
but they are not necessarily all separate problems.
If one pair of libraries is duplicated,
many classes contained in those libraries may all be displayed together as duplicate class errors.
Cause 1: The Same Library Has Been Added Twice
The simplest cause is a case in which
the same library has been added multiple times.
For example,
the same dependency may be written more than once in the module’s Gradle configuration.
dependencies {
implementation("com.example:sample-library:1.0.0")
implementation("com.example:sample-library:1.0.0")
}
In this case,
remove the unnecessary dependency.
dependencies {
implementation("com.example:sample-library:1.0.0")
}
Check
build.gradle
or
build.gradle.kts
to make sure that the same library has not been added multiple times.
Cause 2: Different Versions of the Same Library Are Included
Different versions of the same library may be included in the project
through different dependencies.
For example,
the app may directly specify a newer version,
while another library has an older version as a dependency.
implementation("com.example:library-a:2.0.0")
implementation("com.example:library-b:1.0.0")
If
library-b
internally uses
library-a:1.0.0,
duplication may occur depending on the combination of dependencies.
In this case,
you need to check which library is adding which dependency.
Check Gradle Dependencies
If you do not know where the duplicate dependency is coming from,
check the Gradle dependency tree.
From the Terminal in Android Studio,
run a command such as the following according to your project.
./gradlew app:dependencies
On Windows,
depending on the environment,
run it as follows.
gradlew app:dependencies
When executed,
the dependencies used by the app module are displayed in a tree format.
Look for the library name shown in the error
and check which library is adding it.
Check a Specific Dependency in Detail
If you want to investigate in detail where a particular library is being added from,
you can use Gradle’s
dependencyInsight.
./gradlew app:dependencyInsight \
--dependency library-name \
--configuration debugRuntimeClasspath
This allows you to check through which path
the specified dependency is being added.
Depending on the build type you are using,
you may specify
releaseRuntimeClasspath
or another configuration instead of
debugRuntimeClasspath.
Solution 1: Remove Unnecessary Dependencies
If one of the duplicate libraries does not need to be added directly by the app,
remove the unnecessary dependency.
For example,
a library may have been added directly
even though the same library is already being loaded through another library.
dependencies {
implementation("com.example:library-a:1.0.0")
implementation("com.example:library-b:1.0.0")
}
If
library-b
internally includes
library-a
and there is no need to add it directly from the app,
you may be able to remove the direct specification of
library-a.
Solution 2: Exclude Duplicate Dependencies with exclude
If another duplicate dependency is being added from inside a required library,
you can exclude it using Gradle’s
exclude.
In Kotlin DSL,
for example, write it as follows.
implementation("com.example:library-a:1.0.0") {
exclude(group = "com.example", module = "duplicate-library")
}
In Groovy DSL,
it takes the following form.
implementation("com.example:library-a:1.0.0") {
exclude group: "com.example", module: "duplicate-library"
}
This excludes
duplicate-library
that is added through the specified library.
Note:
If you remove a dependency with
exclude,
runtime errors or other problems may occur if the original library requires that dependency.
Do not exclude a dependency simply because it is duplicated;
first check which library should be retained before configuring it.
Solution 3: Align Library Versions
If different versions of libraries are mixed,
consider aligning them to the same version series as much as possible.
For example, when using multiple related libraries,
dependency inconsistencies may occur because only some of them are using older versions.
implementation("com.example:library-core:2.0.0")
implementation("com.example:library-ui:2.0.0")
Check the library’s official documentation and other information,
and use a compatible combination of versions.
Cause 3: Old Support Library and AndroidX Are Mixed
In older projects,
mixing the Android Support Library and AndroidX
may cause duplicate classes or dependency problems.
For example,
this may occur when a library that uses the old Support Library
and a library that uses AndroidX are included at the same time.
If you are migrating the project to AndroidX,
also check settings such as
gradle.properties.
android.useAndroidX=true
android.enableJetifier=true
However,
depending on the Android Gradle Plugin and libraries being used,
there may also be configurations that do not require Jetifier.
If you are using an older library,
check whether a newer version that supports AndroidX is available.
Cause 4: JAR or AAR Files Have Been Added Manually
If JAR or AAR files have been added directly to the
libs
folder,
and the same library has also been added as a Gradle dependency,
the classes may be loaded twice.
For example,
suppose the following file exists in the project.
app/libs/sample-library.jar
If the same library is also specified in Gradle,
the same classes may be duplicated.
implementation("com.example:sample-library:1.0.0")
In this case,
check whether you want to use the JAR or AAR manually
or use it through Gradle,
and remove the unnecessary one.
Check the libs Folder
If you do not know the cause,
also check the
libs
folder inside the app module.
app/libs/
If old JAR or AAR files remain in this folder,
they may duplicate libraries currently being retrieved through Gradle.
Check this especially if a library that was previously added manually
was later changed to a Gradle dependency.
Cause 5: A Local Module and an External Library Are Duplicated
Classes may also be duplicated when a local module with the same functionality
exists in the project
and the same library is also added as an external dependency.
For example,
suppose it is added as a local module as follows.
implementation(project(":samplelibrary"))
And the published version of the same library is also added.
implementation("com.example:samplelibrary:1.0.0")
If the same classes are included in both,
use only one of them.
Cause 6: Firebase or Google Play services Dependencies Are Inconsistent
With services such as Firebase and Google Play services,
which use many related libraries,
problems may occur depending on the combination of dependencies.
If you are using older and newer versions of libraries at the same time,
check the dependency configuration.
If you are using Firebase,
you can also use the Firebase BoM to manage the versions of related libraries.
implementation(platform("com.google.firebase:firebase-bom:VERSION"))
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-auth")
Specify the Firebase BoM version you want to use in
VERSION.
Cause 7: An Old Library Contains Another Library Internally
Some older libraries may directly include required dependency libraries
inside an AAR or JAR.
If you use such a library
and also add the same dependency library through Gradle,
identical classes may be duplicated.
In this case,
check whether a newer version of the library is available,
and if possible, update to a newer version with improved dependency management.
Check Whether You Created the Same Class Yourself
In addition to dependencies,
also check whether classes with the same fully qualified class name
exist in the project’s source code itself.
For example,
duplication occurs if multiple classes with the same name exist in the same package.
com.example.app.SampleClass
Also check cases such as when an old file remains after copying a file,
or when both Java and Kotlin versions of the same class remain in the project.
Run Gradle Sync After Fixing Dependencies
If you modify dependencies in
build.gradle
or
build.gradle.kts,
run Gradle Sync.
Synchronize Gradle in Android Studio
to apply the new dependency configuration to the project.
After that,
build the app again and check whether
Duplicate class found in modules
has been resolved.
Also Check Clean Project and Rebuild Project
If the error remains even after fixing the dependencies,
old build results may still remain.
From the
Build
menu in Android Studio,
perform the equivalent of Clean or Rebuild available in the version of Android Studio you are using.
After that,
build the app again and check whether the error remains.
If Duplicate class found in modules Still Cannot Be Resolved
If the error is not resolved,
check the dependencies one by one based on the class names and module names
displayed in the error message.
- Check the full
Duplicate classerror message in Build Output. - Check the two libraries displayed under
found in modules. - Check the dependencies with
./gradlew app:dependencies. - If necessary, check the dependency source with
dependencyInsight. - Check whether the same library has been directly added twice.
- Check whether different versions of the same library are included.
- Check JAR and AAR files in the
libsfolder. - Check whether a local module and an external library are duplicated.
- If necessary, remove unnecessary dependencies or configure
exclude. - Build again after Gradle Sync.
When a Large Number of Duplicate class Errors Are Displayed
Dozens or hundreds of
Duplicate class
errors may be displayed in Build Output.
Even in this case,
it does not necessarily mean that every class must be fixed one by one.
If an entire library is duplicated,
many classes contained in that library will all be displayed as
Duplicate class.
Check the
found in modules
information shown in the first few errors,
and if the same two libraries are repeatedly displayed,
resolve the duplication of those dependencies.
Even if a large number of
Duplicate class
errors are displayed,
it does not necessarily mean that a large amount of source code must be modified.
Fixing a single duplicated dependency may resolve many errors at once.
Summary
Android Studio’s
Duplicate class found in modules
is an error that occurs when the same class is included in multiple libraries or modules.
First, check the
found in modules
information displayed in Build Output
and identify which two libraries are duplicated.
Then check for duplicate registration of the same library,
mixed versions,
transitive dependencies,
manually added JAR or AAR files,
duplication with local modules,
and other possible causes.
If you do not know the source of the dependency,
using
./gradlew app:dependencies
or
dependencyInsight
allows you to check which library is adding the dependency.
When resolving
Duplicate class found in modules,
it is important not to delete the class shown in the error itself,
but to check
“why the same class is being loaded from two modules.”
Identify the duplicated dependency and resolve it by removing unnecessary dependencies,
aligning versions,
or using
exclude
as needed.
