为什么我得到重复 Class 当 运行 我的 Android 项目

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

我正在为我的应用程序添加导航抽屉。我收到错误。应用程序 gradle 同步得很好。但是当我 运行 应用程序时,我收到一堆重复的 class 错误。我认为这可能是因为我添加了相互冲突的依赖项并且我使用的是 v7 28.0.0 并且一些错误提到了 app: 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 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 文件

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

异常意味着,在 2 个或更多不同的依赖项中存在重复的 类,编译器将无法区分应该在 run-time 处使用它们中的哪一个,并抛出异常.

最常见的是,当您尝试导入带有所需库的模块时,口是心非就会发生。 (传递依赖)

您必须 excludebuild.gradle 的库中复制 类。 如日志所示,support-core-uisupport-compat 模块重复了 类.

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

有时你不需要排除任何东西,你只需要将导入的模块更改为不带依赖项的模块

其他情况导致重复类是当你添加了*.jar到项目libs 目录。因此,如果项目中没有开始使用它们,您需要删除它们。

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

我看到有一些解决方案提到使用这两行可以解决问题但是如果你已经迁移到 Androidx 它会默认启用。

android.useAndroidX=true
android.enableJetifier=true

Jetifier 是

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.

有关更多信息,请查看 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

如果您在排除 类 时遇到问题,请检查此线程:How do I exclude...

去gradle.properties写下这两行代码:

android.useAndroidX=true
android.enableJetifier=true

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

我通过使用相同的项目和包名称创建一个新项目,然后将文件从以前的项目复制到新项目,解决了这个问题。

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

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