Ad

Gradle "Duplicate Class" Error When Adding UCrop And Cometchat Libraries

- 1 answer

When I add the UCrop library version 2.2.3 and Cometchat SDK version 1.6.+ I get the following error :

Duplicate class okhttp3.Address found in modules okhttp-3.11.0.jar (com.squareup.okhttp3:okhttp:3.11.0) and okhttp-3.12.0.jar (com.cometchat:pro-android-chat-sdk:1.6.0)

The problem is that none of the previously asked questions have answers that solved my problem because most of them use the 'compile' method which is now deprecated.

I read many questions here on stackoverflow about the same topic, including this , this.

I've also tried excluding okhttp3 library from one of the packages so that only one is used , using

implementation('com.github.yalantis:ucrop:2.2.3' )
    {       
exclude group: 'com.cometchat', module: 'okhttp3'
  }

I would appreciate it if someone could explain to me how excludes work in gradle and what's wrong with the code that I wrote.

Ad

Answer

Instead of excluding okhttp3 from com.cometchat group try doing this

implementation('com.cometchat:pro-android-chat-sdk:1.6.0') {
        configurations {
            compile.exclude module: 'okhttp'
        }
    }

The conflict is due to your both UCrop and CometChat dependencies internally uses okhttp library.To resolve this problem you have to exclude conflicting library.

Excluding transitive dependency can be done two different ways.

  1. Exclude transitive dependency by configuration
  2. Exclude transitive dependency by dependency

To read more about gradle dependency conflict cause and solution you can check out this link https://www.concretepage.com/build-tools/gradle/gradle-exclude-transitive-dependency-example

Ad
source: stackoverflow.com
Ad