commons-logging defines classes that conflict with classes now provided by Android after Android Studio Update

AndroidAndroid StudioAndroid Gradle-PluginAndroid Studio-3.0Android Gradle-3.0

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Add to build.gradle located in app module

configurations {
    all {
        exclude module: 'httpclient'
    }
}

Solution 2 - Android

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'
    }
}

Solution 3 - Android

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'
}

Solution 4 - Android

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

Solution 5 - Android

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)

Solution 6 - Android

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

configurations {
    all {
        exclude module: 'commons-logging'
    }
}

Solution 7 - Android

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'

Solution 8 - Android

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

Solution 9 - Android

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:

Solution 10 - Android

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"

Solution 11 - Android

Add this then sync your gradle

configurations {
    all*.exclude group: 'com.google.guava', module: 'listenablefuture'
    all*.exclude module: 'httpclient'
    all*.exclude module: 'commons-logging'
}

Solution 12 - Android

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

instead found another answer: https://stackoverflow.com/questions/47018437/error-json-defines-classes-that-conflict-with-classes-now-provided-by-android/47410498

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreaFView Question on Stackoverflow
Solution 1 - AndroidSilverstormView Answer on Stackoverflow
Solution 2 - AndroidsrsView Answer on Stackoverflow
Solution 3 - AndroidRaja PeelaView Answer on Stackoverflow
Solution 4 - AndroidkhammamiView Answer on Stackoverflow
Solution 5 - AndroidSergio ViudesView Answer on Stackoverflow
Solution 6 - AndroidExigente05View Answer on Stackoverflow
Solution 7 - AndroidMuhammad Etisam ZafarView Answer on Stackoverflow
Solution 8 - AndroidRohit SharmaView Answer on Stackoverflow
Solution 9 - AndroidMartin VysnyView Answer on Stackoverflow
Solution 10 - AndroidKishan SolankiView Answer on Stackoverflow
Solution 11 - AndroidMihae KheelView Answer on Stackoverflow
Solution 12 - Androidamir hossein kazemiView Answer on Stackoverflow