Why I'm Getting Duplicate Class When Running My Android Project

The exception means, There were duplicated classes in 2 or more different dependencies and compiler wouldn't be able to distinguish which of them should be used at run-time and the exception was thrown.

Most often, Duplicity happens when you are trying to import modules that carrying their required libraries.(transitive dependencies)

You have to exclude duplicated classes from libraries in the build.gradle. As Log shows, support-core-ui and support-compat modules have duplicated classes.

apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }
    configurations {
        all { // You should exclude one of them not both of them 
            exclude group: "com.android.support", module: "support-core-ui"
            exclude group: "com.android.support", module: "support-compat"
        }
    }
}

Sometimes you don't need to exclude anything and you only need to change imported module to that one that does not bring its dependencies.

Other situation that causes duplicated classes is when you have add *.jar to the project libs directory. Therefore, You need to delete them if they are not begin used in the project.

project->app->libs->*.jar

i see there are some solutions mentioned using these 2 lines will resolve the problem But if you've migrated to Androidx it would be enabled by default.

android.useAndroidX=true
android.enableJetifier=true

Jetifier is

Jetifier tool migrates support-library-dependent libraries to rely on the equivalent AndroidX packages instead. The tool lets you migrate an individual library directly, instead of using the Android gradle plugin bundled with Android Studio.

And for more information take a look at Exclude transitive dependencies

As an app grows in scope, it can contain a number of dependencies including direct dependencies and transitive dependencies (libraries which your app's imported libraries depend on). To exclude transitive dependencies that you no longer need, you can use the exclude keyword

If you have problems excluding classes, check this thread: How do i exclude...


See if adding this dependency works:

implementation 'com.android.support:support-v4:28.0.0'

Go to gradle.properties and write these two lines of code:

android.useAndroidX=true
android.enableJetifier=true