简体   繁体   English

在模块 classes.jar 中发现重复的类

[英]Duplicated classes found in modules classes.jar

Working with " Cxense SDK for Android" , I'm getting the message of duplicated class:使用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) 

This is my app level build.gradle configuration:这是我的应用级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'
}

How could I avoid this duplicated classes that are surely contained in Cxense SDK?如何避免 Cxense SDK 中肯定包含的重复类?

There are two ways to fix your issue.有两种方法可以解决您的问题。

  1. Excluding duplicated dependencies while implementation individually ,单独implementation排除重复的依赖项

  2. Excluding duplicated dependencies from every implementation s in generic way .通用方式从每个implementation排除重复的依赖项

Let's first understand the problem :我们先来了解一下问题:

Here, in your case artifact com.android.support is duplicated, because your app module uses version : 27.1.1 while your artifact com.cxpublic:cxense-android:1.0.1 is having internal dependency of com.android.support uses version : 22.2.0 .在这里,在您的情况下,工件com.android.support是重复的,因为您的应用程序模块使用版本: 27.1.1而您的工件com.cxpublic:cxense-android:1.0.1具有com.android.support使用版本的内部依赖性: 22.2.0

So, you should remove one of them manually (removing older or lower version is recommended) .因此,您应该手动删除其中之一(建议删除旧版本或较低版本) Let's try to remove it !让我们尝试删除它!


By first approach:通过第一种方法:

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

Putting exclude for group com.android.support in our artifact com.cxpublic:cxense-android:1.0.1 will not get imported for support-v4 libraries when we use our implementation on this artifact.当我们在这个工件上使用我们的实现时,将com.android.support exclude放在我们的工件com.cxpublic:cxense-android:1.0.1中将不会被导入到support-v4库中。

So, issue would be resolved by manually putting this block to every artifact having this conflict.因此,问题将通过手动将此块放置到每个具有此冲突的工件来解决。

By Second way:第二种方式:

We can remove included dependencies and replace them with one package with latest number.我们可以删除包含的依赖项,并用一个最新编号的包替换它们。 In our case, it is support-v4 with different version.在我们的例子中,它是不同版本的support-v4 So, go to the android block of app level build.gradle and put below block there to remove duplicated support-v4 from all artifacts, so that we can have distinct dependency.因此,转到应用程序级别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
}

If there are duplicates, use exclude :如果有重复,请使用exclude

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

Or remove implementation 'com.android.support:appcompat-v7:27.1.1' in favour of support-v4 .或者删除implementation 'com.android.support:appcompat-v7:27.1.1'以支持support-v4

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

You can simple delete the caches folder from below path and rebuild the project.您可以简单地从路径下方删除缓存文件夹并重建项目。 Then it builds everything afresh.然后它重新构建一切。

C:\Users\%USERNAME%\.gradle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM