17

Working with "Cxense SDK for Android", I'm getting the message of duplicated class:

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:

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?

Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    do you post all dependencies here, I just add in a sample project and I don't get any error – Linh Jul 05 '19 at 04:31
  • Do you have the same configuration ? compileSdkVersion 27, targetSdkVersion 27 etc? – Jorgesys Jul 05 '19 at 14:56
  • 1
    More info:https://stackoverflow.com/questions/56029393/why-im-getting-duplicate-class-when-running-my-android-project/56029604#56029604 – A Farmanbar Nov 15 '19 at 00:30

3 Answers3

36

There are two ways to fix your issue.

  1. Excluding duplicated dependencies while implementation individually,

  2. Excluding duplicated dependencies from every implementations in generic way.

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.

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.

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

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
}
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
25

If there are duplicates, use 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.

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

RonTLV
  • 2,376
  • 2
  • 24
  • 38
aucd29
  • 476
  • 4
  • 6
  • @aucd29 i am using this : implementation 'com.google.android.libraries.places:places:2.0.0' implementation 'com.google.android.libraries.places:places-compat:2.0.0' Getting this error Duplicate class com.google.android.libraries.places.widget.AutocompleteActivity found in modules classes.jar (com.google.android.libraries.places:places-compat:2.0.0) and classes.jar (com.google.android.libraries.places:places:2.0.0) – Mahesh Shahane Aug 15 '19 at 09:39
  • 1
    can you please tell me in build.gradle where to put this line ?? in packagingoption or in dependencies ? – Arslan Kaleem Nov 01 '21 at 18:40
-2

You can simple delete the caches folder from below path and rebuild the project. Then it builds everything afresh.

C:\Users\%USERNAME%\.gradle
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Sukumar k
  • 17
  • 3