Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly import HttpClient from org.apache on Android using gradle build file?

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?

like image 365
xrd Avatar asked Apr 02 '13 22:04

xrd


People also ask

What is Apache HttpClient?

The Apache HttpClient library allows to handling HTTP requests. To use this library add a dependency to your Maven or Gradle build file. You find the latest version here: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient. You retrieve and send data via the HttpClient class.


5 Answers

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"
like image 161
Hiery Nomus Avatar answered Sep 29 '22 17:09

Hiery Nomus


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'
}
like image 32
twelve17 Avatar answered Sep 29 '22 18:09

twelve17


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

like image 35
Marek Sebera Avatar answered Sep 29 '22 17:09

Marek Sebera


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'
    }
}
like image 38
Sophia Avatar answered Sep 29 '22 18:09

Sophia


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

like image 26
Xavier Ducrohet Avatar answered Sep 29 '22 17:09

Xavier Ducrohet