【问题标题】:Duplicated classes found in modules classes.jar在模块 classes.jar 中发现重复的类
【发布时间】:2019-11-03 19:30:48
【问题描述】:

使用 "Cxense SDK for Android",我收到重复类的消息:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (**com.android.support:support-compat:27.1.1**) and classes.jar (**com.android.support:support-v4:22.2.0**)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules classes.jar (com.android.support:support-compat:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules classes.jar (com.android.support:support-compat:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 
Duplicate class android.support.v4.app.ListFragment found in modules classes.jar (com.android.support:support-fragment:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 
Duplicate class android.support.v4.app.ListFragment$1 found in modules classes.jar (com.android.support:support-fragment:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 
Duplicate class android.support.v4.app.ListFragment$2 found in modules classes.jar (com.android.support:support-fragment:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 
Duplicate class android.support.v4.app.LoaderManager found in modules classes.jar (com.android.support:support-fragment:27.1.1) and classes.jar (com.android.support:support-v4:22.2.0) 

这是我的应用级build.gradle配置:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.tototita.cxensetestapp"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    packagingOptions {
        exclude 'META-INF/LICENSE'
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }


}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1' 
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

     //**CXsense
     implementation 'com.cxpublic:cxense-android:1.0.1'
}

如何避免 Cxense SDK 中肯定包含的重复类?

【问题讨论】:

  • 你把所有依赖贴在这里吗,我只是添加了一个示例项目,我没有收到任何错误
  • 你们有同样的配置吗? compileSdkVersion 27, targetSdkVersion 27 等等?

标签: java android android-studio gradle


【解决方案1】:

如果有重复,使用exclude:

implementation ('com.cxpublic:cxense-android:1.0.1') {
    exclude group: "com.android.support", module: "support-v4"
}

或删除implementation 'com.android.support:appcompat-v7:27.1.1' 以支持support-v4

见:https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991

【讨论】:

  • @aucd29 我正在使用这个: implementation 'com.google.android.libraries.places:places:2.0.0' implementation 'com.google.android.libraries.places:places-compat:2.0 .0' 出现此错误 在模块 classes.jar (com.google.android.libraries.places:places-compat:2.0.0) 和 classes.jar 中找到重复的类 com.google.android.libraries.places.widget.AutocompleteActivity (com.google.android.libraries.places:places:2.0.0)
  • 你能告诉我在 build.gradle 中把这条线放在哪里吗?在包装选项或依赖项中?
【解决方案2】:

有两种方法可以解决您的问题。

  1. 排除重复依赖implementation单独

  2. 通用方式从每个implementations 中排除重复的依赖项

我们先搞清楚问题:

在这里,在您的情况下,工件 com.android.support 是重复的,因为您的 app 模块 使用版本:27.1.1 而您的工件 com.cxpublic:cxense-android:1.0.1 具有 com.android.support 的内部依赖,使用版本:22.2.0

因此,您应该手动删除其中一个(建议删除旧版本或更低版本)。让我们尝试删除它!


第一种方法:

implementation ('com.cxpublic:cxense-android:1.0.1') {
    exclude group: "com.android.support", module: "support-v4"
}

在我们的工件com.cxpublic:cxense-android:1.0.1 中为 group com.android.support 放置 exclude 将不会为 support-v4 导入 strong> 当我们在这个工件上使用我们的实现时的库。

因此,可以通过手动将此块放入每个有此冲突的工件来解决问题。

第二种方式:

我们可以删除包含的依赖项,并用一个最新编号的包替换它们。在我们的例子中,它是不同版本的 support-v4。因此,转到应用级别 build.gradle 的 android 块并将其放在下面的块中以从所有工件中删除重复的 support-v4,以便我们可以有不同的依赖关系。

android {
    // Some other stuffs
    configurations {
        all*.exclude module: 'support-v4' // This removes all other versions of `support-v4` if gets duplicated from all the artifacts.
    }
    // Rest of other stuffs
}

【讨论】:

    【解决方案3】:

    您可以简单地从下面的路径中删除缓存文件夹并重建项目。然后它重新构建一切。

    C:\Users\%USERNAME%\.gradle
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多