为什么在运行Android项目时会出现重复的类

时间:2019-05-07 19:36:33

标签: android android-studio-3.0

我正在向我的应用添加导航抽屉。而且我遇到了错误。应用程序gradle同步就好了。但是当我运行该应用程序时,出现了一堆重复的类错误。我认为这可能是因为我添加了相互冲突的依赖项,并且我使用的是v7 28.0.0,并且某些错误提到了应用程序:v4。我在网上看到的所有示例都使用v7 28.0.0,尽管我在使用v4的main_activity.xml中也有。不知道是否与错误有关。 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)

渐变文件

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

6 个答案:

答案 0 :(得分:1)

此错误意味着某些库具有相同的方法,并且编译器无法区分应在项目中使用哪个库并引发错误。 您必须在Gradle文件中排除冲突的库。 如Log所示,classes.jar模块具有重复的类。

    android{
    ...
        configurations {

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

这将解决异常。

答案 1 :(得分:1)

转到gradle.properties并编写以下两行代码:

android.useAndroidX = true android.enableJetifier = true

答案 2 :(得分:1)

这些行不能解决问题:

android.useAndroidX=true
android.enableJetifier=true

答案 3 :(得分:0)

请将com.google.android.gms:play-services更新到最新版本。会起作用的。

答案 4 :(得分:0)

我通过创建一个具有相同项目名称和ID的新项目,然后从先前项目中复制文件来解决了这个问题。

答案 5 :(得分:0)

查看添加此依赖项是否有效:

实现'com.android.support:support-v4:28.0.0'