38

I am seeing this error when I try to run "gradle build"

WARNING: Dependency org.apache.httpcomponents:httpclient:4.2.3 is ignored for the default configuration as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage with jarjar to change the class packages
:prepareFreeDebugDependencies
:compileFreeDebugAidl UP-TO-DATE
:generateFreeDebugBuildConfig UP-TO-DATE
:mergeFreeDebugAssets UP-TO-DATE
:compileFreeDebugRenderscript UP-TO-DATE
:mergeFreeDebugResources UP-TO-DATE
:processFreeDebugManifest UP-TO-DATE
:processFreeDebugResources UP-TO-DATE
:compileFreeDebug
/home/xrdawson/Projects/Foo/Bar/src/main/java/com/Foo/app/PixActivity.java:20: error: package org.apache.http.entity.mime does not exist
import org.apache.http.entity.mime.HttpMultipartMode;
                              ^

The end of my build.gradle looks like this:

    repositories {
        mavenCentral()
    }

    dependencies { 
        compile fileTree(dir: 'libs', include: '*.jar')
        compile "org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.3"
        compile "com.madgag:markdownj-core:0.4.1"
//      compile "org.apache.httpcomponents:com.springsource.org.apache.httpcomponents.httpclient:4.2.1"
        compile 'org.apache.httpcomponents:httpclient:4.2.3'
        compile "com.google.android:support-v4:r6"
    } 
}

Why is the compile process ignoring HttpClient, but then failing to compile?

xrd
  • 4,019
  • 6
  • 30
  • 39

6 Answers6

79

I think the httpclient library doesn't include the mime parts, those are in httpmime. This is a transitive dependency of httpclient, but as that is ignored, it won't be taken into account.

Try adding this dependency:

compile "org.apache.httpcomponents:httpmime:4.2.3"
Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • 2
    Part of httpmime 4.2+ depends on org.apache.http.entity.ContentType that is present in httpcore 4.2+ but NOT present in android.jar (for, at least, APIs 9...19). Hence, you might want to restrict yourself to depend on org.apache.httpcomponents:httpmime:4.1.3 to be fine. – averasko Sep 01 '14 at 14:10
  • From google,it says that transitive dependency is now solved.Since version 1.0 of the Gradle plugin, we have moved to Gradle 2.2+ which has support for advanced custom dependency resolution. It'll allow you to override any transitive dependency with a different one. This should allow you to fix issues where you depend transitively on a broken dependency. – anshad Apr 03 '15 at 11:07
  • 1
    with this i am not able to use `MultipartEntityBuilder`. Any alternatives? Finding solution from past 2 days... – Jimit Patel Sep 28 '15 at 06:28
  • 2
    @JimitPatel - add follwing in your `gradle` `compile ('org.apache.httpcomponents:httpmime:4.3.5') { exclude group: 'org.apache.httpcomponents', module: 'httpclient' } ` – pRaNaY Nov 30 '15 at 10:01
  • I found out the solution... Here is my version - http://stackoverflow.com/questions/32796770/upload-image-via-volley-throwing-error – Jimit Patel Nov 30 '15 at 10:22
17

Adding http-mime as a dependency causes httpclient to be included as a transitive dependency, which, for me, resulted in the same warnings as the OP. I had to tell gradle to ignore the transitive dependency:

compile ('org.apache.httpcomponents:httpmime:4.3.5') {
    // avoid "is ignored for the default configuration X" warnings 
    // since httpclient is included in the android SDK.
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
twelve17
  • 688
  • 8
  • 20
13

For Android, there is now available HttpClient 4.3.X repackaged Maven distribution

Project repo: https://github.com/smarek/httpclient-android
Maven tag: cz.msebera.android:httpclient:4.3.+
Published to Maven Central repository

Which in version 4.3.3 includes HttpCore, HttpClient, HttpClient-Cache and HttpMime (all of same version)

Disclaimer: I'm author of said project

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
3

Adding to this I solved the Issue by Using This, if your compileSdkVersion is 19(IN MY CASE)

compile ('org.apache.httpcomponents:httpmime:4.3'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'

else if your compileSdkVersion is 23 then use

android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    }
}
Sophia
  • 1,046
  • 1
  • 12
  • 15
2

Because the official Android APIs includes httpclient we remove all dependency on httpclient, including its transitive dependency.

if you really want to use httpclient, I'd repackage it with jarjar, renaming the packages and use this instead.

As for httpmime, it looks like it's not actually in android.jar so we could avoid filtering it out, but for now you would have to add it manually.

We'll probably want to tweak this before the build system goes 1.0

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • 1
    I'm also suffering from this problem. You're answer is a bit confusing. Are you saying that the version of gradle with android studio forces this behavior? There's no way around it short of jarjar? – Amaron Aug 16 '14 at 18:39
  • 1
    I am also having this problem. How to include multipart upload support without having to pull in httpmime and get bogged down in this mess? – Greg Ennis Sep 25 '14 at 16:05
0

Just add this to build.gradle(Module: app) file:

dependencies {
...
   implementation "org.apache.httpcomponents:httpmime:4.5.6"
}
Hossein Yousefpour
  • 3,827
  • 3
  • 23
  • 35