Why am I Getting a Duplicate Class When Running My Android Project?
Asked Answered
L

7

22

I'm in the process of adding a navigation drawer to my app. and I'm getting errors. The app gradle synchs just fine. But when I run the app I get a bunch of duplicate class errors. I think it might be because I have conflicting dependencies added and that I'm using v7 28.0.0 and some of the errors mention app: v4. all the examples I've seen online use v7 28.0.0 even though I have this in main_activity.xml which uses v4. I don't know if it's got something to do with the error.

android.support.v4.widget.DrawerLayout:

Caused by: com.android.ide.common.workers.WorkerExecutorException: 1 exception was raised by workers:
java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$Delegate found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$DelegateProvider found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActionBarDrawerToggle$SlideDrawable found in modules classes.jar (com.android.support:support-core-ui:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat$1 found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)
Duplicate class android.support.v4.app.ActivityCompat$OnRequestPermissionsResultCallback found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:24.0.0)

gradle file:

apply plugin: 'com.android.application'

android {    
    
    compileSdkVersion 28
    defaultConfig {
        applicationId "org.pctechtips.netdroid"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 8
        versionName "1.7"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled = false
        signingConfig signingConfigs.config
    }
    buildTypes {
        release {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable false

        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    /*androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
                        exclude group: 'com.android.support', module: 'support-annotations'
                        firebase
                        implementation 'com.google.firebase:firebase-core:10.2.1'
                    })*/
    //    compile 'com.android.support:appcompat-v7:25.3.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    /*google play plugin for adMob*/
    implementation 'com.google.android.gms:play-services:10.2.1'
    implementation 'commons-net:commons-net:3.6'
    implementation 'org.samba.jcifs:jcifs:1.3.3'
}
Lunation answered 7/5, 2019 at 19:36 Comment(0)
I
42

The exception means, There were duplicated classes in 2 or more different dependencies and the 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 the imported module to that one that does not bring its dependencies.

Other situation that causes duplicated classes is when you have added *.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...

Inbred answered 7/5, 2019 at 19:52 Comment(3)
How to find the conflicting library?.. I just included some code for navigation drawer and it gave these errors. App was working just fineLunation
how this duplicates class got there? by using two different libraries that have same classes. wouldn't be easier to remove libraries from graddle so they get remove from external libraries and then add it again?Lunation
most of the duplicate come from this library android.support.v4.app how can I exclude it?Lunation
P
13

See if adding this dependency works:

implementation 'com.android.support:support-v4:28.0.0'
Paregmenon answered 28/7, 2019 at 19:8 Comment(1)
Yes! Perfect solution here! I was facing that problem implemeting Signature Pad in a RecyclerView and this solved everything, happy christmas!Prospero
S
11

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

android.useAndroidX=true
android.enableJetifier=true
Scofflaw answered 21/6, 2019 at 16:11 Comment(1)
you saved my life. Can't belive - enableJetifier is workingFujio
C
2

I tried all above solution, but nothing worked in my case. What worked for me is.

I got this problem resolved by creating a new project with the same project and package name and then copying files from the previous project to new one.

Castorina answered 16/7, 2019 at 13:19 Comment(1)
I don't understand why people are disliking the answer? I have given the solution that worked for me for the same problem. This is making me scared for giving answers from next timeCastorina
M
0

Please update com.google.android.gms:play-services to latest version. it wil work.

Martine answered 27/6, 2019 at 13:1 Comment(1)
Can you kindly explain why it will work and what's wrong with OP's Play Services version?Ruphina
I
0

After finding the root cause of the two dependencies colliding with different versions of the same library, it's better practice I think to enforce a version rather than excluding it.

For me, it worked like this:

    // Force androidx.work:work-runtime andwork-runtime-ktx  to use version 2.8.0
    // due to transitive dependancy conflict with datadoghq

    api(group: "androidx.work", name: "work-runtime") {
        version {
            strictly "2.8.0"
        }
    }
    api(group: "androidx.work", name: "work-runtime-ktx") {
        version {
            strictly "2.8.0"
        }
    }
Interdictory answered 14/12, 2023 at 20:5 Comment(0)
B
0

In my case I was using RoomDb and in which I used implementation(libs.roomRuntime) implementation(libs.roomCompiler) implementation(libs.roomKtx) in module gradle, which was causing this error then I changed it to implementation(libs.roomRuntime) annotationProcessor(libs.roomCompiler) implementation(libs.roomKtx) which starts working

Binnie answered 9/7 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.