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.
Add to build.gradle located in app module
configurations {
all {
exclude module: 'httpclient'
}
}
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'
}
}
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'
}
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
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)
If you want to continue with async-http then add below following code only in app/build.gradle
configurations {
all {
exclude module: 'commons-logging'
}
}
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'
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
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:
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"
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'
}
}
Add this then sync your gradle
configurations {
all*.exclude group: 'com.google.guava', module: 'listenablefuture'
all*.exclude module: 'httpclient'
all*.exclude module: 'commons-logging'
}
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'
}
}
I am getting httpclient-android-4.3.5.1.jar file with one of the aar file that I am adding in my dependencies.
I want to exclude this jar from this dependency, because this is giving me duplicate library error message.
I have tried many things, from
exclude group: 'org.apache.httpcomponents', module: 'httpclient-android'
to
{exclude module: "httpclient-android-4.3.5.1"}
but nothing is helping, the '.jarfile is still there and part ofSDK`
Did you try this also?:
configurations { all { exclude module: 'httpclient' exclude module: 'httpcore' } }
I'm trying to use Simple Framework in my project. Here is the dependency in XML format.
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>2.7.1</version>
</dependency>
I've tried converting the dependency to Gradle acceptable format.
compile 'simpleframework:simple-xml:2.7.1'
But, I don't think it's correct. What is the correct gradle dependecy?
The correct Gradle dependency is:
compile 'org.simpleframework:simple-xml:2.7.1'
For Android, you also have to exclude dependencies as they are already in the Android SDK
compile ('org.simpleframework:simple-xml:2.7.1') {
exclude module: 'stax-api'
exclude module: 'stax'
exclude module: 'xpp3'
}
I'm new to android studio and am trying to import a project from eclipse but I feel like i am running into every single problem possible. The error I'm currently getting is:
Error:Execution failed for task ':jobFlexwithInvoice:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: android/support/v4/view/MotionEventCompatEclair.class
Ive seen similar answers to problems like here and here. But i think I need more help because its just not going away. I've added the exception to just about everything in my build.gradle file, including the actual support 4v dependency just for kicks, and am still getting this error. And also I have multiple build.gradle files because of included projects? It seems like the project build.gradle file can be ignored, I just have to add the exceptions to the module build.gradle files? (which is annoying when someone says to add something to the build.gradle file, im not always sure which one)
Anyway, here are the dependencies sections of each of my build.gradle files as they currently are:
myApp:
dependencies {
compile ('com.android.support:multidex:1.0.0'){
exclude module: 'support-v4'
}
compile project(':facebook') {
exclude module: 'support-v4'
}
compile project(':apptentive')
compile project(':androidsdkui'){
exclude module: 'support-v4'
}
compile ('com.google.android.gms:play-services:+'){
exclude module: 'support-v4'
}
compile files('libs/android-support-v13.jar') {
exclude module: 'support-v4'
}
compile files('libs/DynamicPDF.jar')
compile files('libs/picasso-2.5.0.jar')
compile ('com.android.support:support-v4:22.0.0'){
exclude module: 'support-v4'
}
}
facebook:
dependencies {
compile files('libs/bolts-android-1.2.1.jar')
compile ('com.android.support:support-v4:22.0.0'){
exclude module: 'support-v4'
}
}
androidsdkui:
dependencies {
compile files('libs/appboy.jar')
compile ('com.android.support:support-v4:22.0.0'){
exclude module: 'support-v4'
}
}
there is also an apptentive build.gradle file but it does not have any dependencies. Before adding all of these exceptions I was getting the same error as the one in the first link, now the only difference is its android/support/v4/view/MotionEventCompatEclair.class instead of android/support/v4/util/TimeUtils.class
If there is a way to see where the duplicates are coming from that would be great too, I was unable to find anything with a quick google search.
I updated the question so you can see some of the ways I was doing things wrong. Some of my projects included support v4 and I wasn't excluding them right. The proper way to exclude things from projects is:
compile (project(':facebook')) {
exclude module: 'support-v4'
}
you have to put the extra set of parenthesis around everything after compile. Im assuming the same goes for excluding things from files
ie.
compile file(myfile.jar)
would become
compile (file(myfile.jar)) {
exclude module: 'support-v4'
}
Could you try adding the following in the top-level build.gradle file inside allprojects element? It should look something like below.
allprojects {
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
}
Hope this helps.
My build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.10.0'
}
}
apply plugin: 'android-sdk-manager'
apply plugin: 'android'
apply plugin: 'spoon'
apply plugin: 'robolectric'
android {
compileSdkVersion 19
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
}
buildTypes {
debug {
}
release {
}
}
productFlavors {
first {
//just version codes and packages
}
second {
//just version codes and packages
}
third {
//just version codes and packages
}
}
}
spoon {
debug = true
}
dependencies {
compile project(':app:libs:facebookSDK')
compile 'com.google.android.gms:play-services:5.0.89'
compile 'com.android.support:support-v4:20.0.0+'
compile 'com.google.code.gson:gson:2.2.4'
compile fileTree(dir: 'libs', include: '*.jar')
androidTestCompile fileTree(dir: 'libsTest', include: '*.jar')
androidTestCompile 'com.squareup.spoon:spoon-client:1.1.0'
androidTestCompile 'junit:junit:4.+'
androidTestCompile 'org.robolectric:robolectric:2.3'
}
libsTest: espresso-contrib-1.1-bundled.jar
Now, my error is following:
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/SelfDescribing;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
More detailed log available here:
http://pastebin.com/Yye3cd1c
How do I fix this issue?
UPD:My question now is basically how do I hunt those duplicated parts?
My question now is basically how do I hunt those duplicated parts?
You do this using the log. The log you pasted says:
com.android.dex.DexException: Multiple dex files define
This is most probably due to the conflicting libraries. The log continues with:
Lorg/hamcrest/SelfDescribing;
Here is the conflicting library, hamcrest.
After adding dependencies to your project, if there are common sub-libraries used by your libraries (or as your libraries), this error occurs. So it seems like hamcrest is being used by not only one of your libraries.
We'll learn about this conflict by inspecting dependencies. Of course an efficient inspection needs to be both intuitive and rational at the same time.
Let's start with hamcrest itself. (I'll assume you haven't heard of hamcrest before.)
Let's take a look at Hamcrest project page. Hamcrest defines itself as library of matchers for building test expressions. And the definition says Typical scenarios include testing frameworks, mocking libraries... This is illuminating because you have some dependencies about testing like JUnit, Espresso and Robolectric.
Now we should continue with JUnit dependencies. Seems like JUnit makes use of hamcrest-core. Here is our first hamcrest as a sub-dependency.
Let's continue with Espresso, as you have espresso-contrib-1.1-bundled.jar in libsTest folder.
When we check out espresso-contrib project dependencies, we can see that Espresso makes use of hamcrest heavily.
We probably get the conflicting library in your project, the last step is excluding hamcrest-core from one of our dependencies while adding this dependency. You can achieve this with:
androidTestCompile('junit:junit:4.+') {
exclude module: 'hamcrest-core'
}
I had a similar issue a while ago and if I remember correctly, I fixed it by adding an exclude statement as follows:
androidTestCompile('junit:junit:4.+') {
exclude module: 'hamcrest-core'
}
Inspired from the deckard-gradle sample project, I suggest trying something like this:
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
One trick to hunt those errors is using Android Studio "Navigate > Class..." option.
Given you have this error:
Lorg/hamcrest/SelfDescribing;
then you can search "org.hamcrest.SelfDescribing" so you can have an idea of the .jars using it.