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

别等时光非礼了梦想. 提交于 2019-11-26 12:47:44

问题


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 error. 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 eventhough I have this in main_activity.xml which uses v4. 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)

graddle 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\'
}

回答1:


This error means there are similar libraries and complier won't be able to distinguish which of them should be used in your project and error thrown.

Most often,duplicity happens when you are trying to import modules which carries their required libraries looks like a guest who brings his spoon and fork into party for dinner table!

You must exclude conflicted libraries in your Gradle file. As Log shows, classes.jar module has duplicated classes.

android{
...
  configurations {
     all*.exclude group:'android.support.v4.app' module: ‘classes.jar’
     all*.exclude group:'android.support.v4.accessibilityservice' module: ‘classes.jar’
  }
}

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.

i see there are some solutions mentioned using these 2 lines will resolve the problem. but i didn't test it

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 to 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




回答2:


See if adding this dependency works:

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




回答3:


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

android.useAndroidX=true
android.enableJetifier=true



回答4:


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




回答5:


These lines do not resolve the probleme :

android.useAndroidX=true
android.enableJetifier=true



回答6:


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



来源:https://stackoverflow.com/questions/56029393/why-im-getting-duplicate-class-when-running-my-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!