61

I have updated Android Studio to version 3 and now seems unable to compile my project previously compiled without errors.

The error message is the follow

Error:Error: commons-logging defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. [DuplicatePlatformClasses]

The dependencies are

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:27.0.0'
    compile 'com.android.support:design:27.0.0'
    compile 'com.google.api-client:google-api-client-android:1.23.0' exclude module: 'httpclient'
    compile 'com.google.http-client:google-http-client-gson:1.23.0' exclude module: 'httpclient'
    compile 'com.google.firebase:firebase-core:11.4.2'
}

and error seems caused by

compile 'com.google.api-client:google-api-client-android:1.23.0' exclude module: 'httpclient'
compile 'com.google.http-client:google-http-client-gson:1.23.0' exclude module: 'httpclient'

I already use exclude module: 'httpclient' So why It doesn't compile? Is this a bug of Android Studio 3 and\or included com.android.tools.build:gradle:3.0.0 plugin or I'm missing something? With the previous version no problem to compile exactly the same project.

AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • "So why It doesn't compile?" -- your problem is with `commons-logging`, based on the error. Perhaps there are newer versions of those `com.google` dependencies that you should be using. – CommonsWare Oct 28 '17 at 11:43
  • @CommonsWareI haven't find any newer library these versions are latest October 2017 releases – AndreaF Oct 28 '17 at 11:44
  • Do you have same jars in libs folder? – Gabriele Mariotti Oct 28 '17 at 12:51
  • @GabrieleMariotti absolutely not. I haven't any jars in libs folder. The libraries are only specified in dependencies section in build.gradle (otherwise also the previous Android studio would have refused to build). For this reason I cannot figure why after Android Studio update won't compile. – AndreaF Oct 28 '17 at 13:06

13 Answers13

73

Add to build.gradle located in app module

configurations {
    all {
        exclude module: 'httpclient'
    }
}
Silverstorm
  • 15,398
  • 2
  • 38
  • 52
  • 4
    I am not able to get release apk if exclude httpclient – Ramesh sambu Nov 08 '17 at 09:45
  • @RameshSambu Have you placed the above code in the inner build.gradle of the app module? – Silverstorm Nov 08 '17 at 14:52
  • Yes I added like that – Ramesh sambu Nov 08 '17 at 14:55
  • you can find release apk at app/release – scionoftech Nov 10 '17 at 10:20
  • You have just saved me from a major release crisis. Thank you! – Pkmmte Nov 21 '17 at 19:35
  • In which tag, do I need to put that? – Narendra Singh Dec 18 '17 at 07:49
  • @NerendraJi in configurations. If It's missing just copy paste the code that I have posted. – Silverstorm Dec 18 '17 at 15:57
  • 4
    What if I'm using httpclient? My code used to compile before upgrading to Android Studio 3.0. I'm actually using httpclient in my code, and I don't want to switch to okhttp. – Stevey Dec 20 '17 at 06:19
  • @user3562927 I am also facing and having the same issue as you are having. Did You got any solution for it, as in older version it was working. but after upggading Android Studio and Gradle it is not. – Pravinsingh Waghela Dec 28 '17 at 14:46
  • 1
    configurations { all { exclude module: 'httpclient' exclude module: 'commons-logging' exclude group: 'org.json', module: 'json' exclude group: 'org.apache.httpcomponents' exclude module: 'opengl-api' exclude module: 'xmlParserAPIs' exclude module: 'xpp3' } } this works for me – gbansal Feb 09 '18 at 11:23
  • Ok, but how libraries that use httpclient would work without it? LVL library of Google depends on org.apache.http – ARLabs Mar 07 '18 at 11:34
  • @ARLabs @user3562927 @Pravinsingh-Waghela You need to provide some different `httpclient` library/wrapper. You could try [this one](https://github.com/smarek/httpclient-android) - I was in the same situation yesterday and this library is the only solution that worked for me. You will only have to change all `org.apache.http` imports in your code to `cz.msebera.android.httpclient`. – kibitzerCZ Jun 13 '19 at 09:00
50

If the problem is with commons-logging then it must be excluded too. Add the following code in app/build.gradle

configurations {
    all {
        exclude module: 'httpclient'
        exclude module: 'commons-logging'
    }
}
srs
  • 2,521
  • 16
  • 16
  • 1
    this will exclude `org.apache.http.client.HttpClient` what if our code is already using this library? – Muhammad Saqib Apr 18 '20 at 13:56
  • then you need to find that specific library which is causing this duplicate httpclient import and need to remove the transitive httpclient dependency from that specific library. – srs Apr 21 '20 at 02:27
  • 1
    in my case, `org.apache.http.client.HttpClient` was causing the conflict since android is now itself providing this library. Luckily, I found a clone github repo of `org.apache.http.client.HttpClient` with different name space which solved my problem. – Muhammad Saqib Apr 21 '20 at 11:16
  • 1
    here is the clone, just in case if anyone want to use `implementation group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.2'` – Muhammad Saqib Apr 21 '20 at 11:18
  • thanks @MuhammadSaqib your clone solution (. implementation group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.2' ) saved my day – Parsania Hardik Apr 07 '23 at 12:07
13

Got the same issue. I have done below changes

 configurations {
    all{
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents'
    }
}


packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'org/apache/http/version.properties'
    exclude 'org/apache/http/client/version.properties'
}
Raja Peela
  • 1,366
  • 2
  • 14
  • 26
9

You should replace "compile" with "implementation" as it's deprecated in the latest gradle and exlude "org.apache.httpcomponents" from Google api client libraries:

implementation('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.http-client:google-http-client-gson:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}

this solution was found here: https://developers.google.com/google-apps/activity/v1/quickstart/android

khammami
  • 371
  • 4
  • 14
  • compile: compile library for each which contain current module. implementation: compile library for current module, that's mean implemented library not visible for "parent" modules – Vahe Gharibyan Nov 22 '17 at 23:34
  • but compile is deprecated in the new Android Studio so you'll get a warning to replace it with "implementation" or " api' & that depends on your need – khammami Jul 06 '18 at 12:04
8

Run in terminal, inside project folder:

./gradlew app:dependencies > dependencies.txt

Then check dependencies.txt to find who is using conflictive dependencies and act accordingly (check for updates, get rid of it, or use exclude as suggested by @Silverstorm)

Sergio Viudes
  • 2,714
  • 5
  • 26
  • 44
2

If you want to continue with async-http then add below following code only in app/build.gradle

configurations {
    all {
        exclude module: 'commons-logging'
    }
}
Exigente05
  • 2,161
  • 3
  • 22
  • 42
1

As 'org.apache.httpcomponents:httpclient:4.3.3' is deprecated after SDKversion 23 so

replace this:

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

with

compile 'org.apache.httpcomponents:httpclient:4.3.3'
1

I received these two errors today.

1. commons-logging defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

2. httpclient defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

After struggling for sometime, I figured that I was using a Firebase library which was causing these errors.

implementation platform('com.google.firebase:firebase-bom:29.2.0')

So, I updated it:

implementation platform('com.google.firebase:firebase-bom:29.2.1')

And Invalidated Caches and Restarted the project and it worked like a charm.

Earlier BOM version of Firebase was also working fine.

implementation platform('com.google.firebase:firebase-bom:29.1.0')

Please don't update Firebase BOM version: 29.2.0

Rohit Sharma
  • 1,271
  • 5
  • 19
  • 37
0

I removed commons-logging as suggested above, of course it crashed on some phone with Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;. How can Android claim the commons-logging is conflicting with Android API when the Android API doesn't contain any of those classes?!? There is no org.apache.commons.logging at https://developer.android.com/reference/packages :facepalm:

I've added back implementation 'commons-logging:commons-logging:1.0.4' to the build.gradle - Android Studio underlines it with red but gradle compiles happily. :facepalm:

Android :triple_facepalm:

Martin Vysny
  • 3,088
  • 28
  • 39
  • Those classes are not exposed as public API, though they are used as part of implementation. To see an example of such conflict, check [this issue](https://stackoverflow.com/q/9126567). – Alex Lipov Jun 30 '20 at 13:10
0

if you are facing this issue because of org.apache.httpcomponents:httpmime dependency, then use this in your app level build.gradle file:

implementation('org.apache.httpcomponents:httpmime:4.5.12') {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
implementation "org.apache.httpcomponents:httpcore:4.4.13"
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

I had to join multiple solutions from here. This is what worked for me:

    configurations {
        all*.exclude group: 'com.google.guava', module: 'listenablefuture'
        configureEach {
            exclude module: 'httpclient'
            exclude module: 'commons-logging'
            exclude group: 'commons-logging', module: 'commons-logging'
            exclude group: 'org.apache.httpcomponents'
        }
    }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
-1

Add this then sync your gradle

configurations {
    all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    all*.exclude module: 'httpclient'
    all*.exclude module: 'commons-logging'
}
Mihae Kheel
  • 2,441
  • 3
  • 14
  • 38
-1

in my case android studio couldn't recognize "httpclient" so i couldn't use @Silverstorm answer.

instead found another answer: Error: json defines classes that conflict with classes now provided by Android

which implies to add below could in app build.gradle:

configurations {
    all {
        exclude group: 'org.json', module: 'json'
    }
}