Skip to content

Resolving Duplicate Class Errors in Android Studio

Dom edited this page Apr 15, 2024 · 1 revision

Introduction:

While working on an Android project, you may encounter a java.lang.RuntimeException: Duplicate class error during the build process. This error occurs when there are multiple instances of the same class found in different library modules or JAR files. In this article, we will discuss how to resolve this issue by removing the unused library or excluding the duplicate artifact from one of the libraries.

Step 1: Identify the Duplicate Class

The first step in resolving the duplicate class error is to identify the duplicate class and the libraries or JAR files that contain it. This information can usually be found in the error message, which will list the duplicate classes and the modules or JAR files where they are located. For example:

Duplicate class org.intellij.lang.annotations.Flow found in modules annotations-16.0.1.jar and annotations-java5-15.0.jar

In this case, the duplicate class is org.intellij.lang.annotations.Flow, and it is found in the annotations-16.0.1.jar and annotations-java5-15.0.jar modules.

Step 2: Remove the Unused Library

If you are not using one of the libraries that contains the duplicate class, you can remove it from your project to resolve the error. This can be done by removing the corresponding dependency from your build.gradle file. For example, if you are not using the annotations-java5-15.0.jar module, you can remove the following line from your build.gradle file:

implementation 'org.jetbrains:annotations-java5:15.0'

Step 3: Exclude the Duplicate Artifact

If you are using both libraries that contain the duplicate class, you can exclude the duplicate artifact from one of the libraries. This can be done by adding the following lines to your build.gradle file:

plugins {
    kotlin("jvm") version "1.5.31"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib")) {
        exclude(group = "org.jetbrains", module = "annotations")
    }
}

configurations {
    all {
        exclude(group = "org.jetbrains", module = "annotations")
    }
}

This will exclude the annotations module from the compile configuration, preventing the duplicate class from being included in the build.

Conclusion:

By following the steps outlined in this article, you can resolve the java.lang.RuntimeException: Duplicate class error in Android Studio. This error is usually caused by having multiple instances of the same class in different libraries or JAR files, and can be resolved by removing the unused library or excluding the duplicate artifact from one of the libraries.

FAQs:

  1. Why did I get the duplicate class error in the first place?

The duplicate class error is usually caused by having multiple instances of the same class in different libraries or JAR files. This can happen if you accidentally include the same library twice in your project, or if two different libraries contain the same class.

  1. Can I exclude the duplicate artifact from both libraries?

No, you cannot exclude the duplicate artifact from both libraries, as this would result in the class being removed from both libraries. This would cause compilation errors if the class is required by your code.

  1. What should I do if I need both libraries that contain the duplicate class?

If you need both libraries that contain the duplicate class, you can exclude the duplicate artifact from one of the libraries, as described in Step 3. This will prevent the duplicate class from being included in the build, allowing you to use both libraries without encountering the duplicate class error.

  1. Can I use the api configuration instead of implementation to exclude the duplicate artifact?

No, you cannot use the api configuration to exclude the duplicate artifact, as this would also exclude the artifact from any libraries that depend on the library where it is excluded. This would cause compilation errors if the artifact is required by the dependent libraries.

  1. Can I use the annotationProcessor configuration instead of implementation to exclude the duplicate artifact?

Yes, you can use the annotationProcessor configuration instead of implementation to exclude the duplicate artifact, as this will only exclude the artifact from the annotation processing phase of the build. This is useful if the library containing the duplicate artifact is only used for annotation processing, and is not required at runtime.