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

旧巷老猫 提交于 2019-11-27 01:28:12

问题


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?


回答1:


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"



回答2:


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



回答3:


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




回答4:


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



回答5:


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




回答6:


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

dependencies {
...
   implementation "org.apache.httpcomponents:httpmime:4.5.6"
}


来源:https://stackoverflow.com/questions/15775747/how-do-i-properly-import-httpclient-from-org-apache-on-android-using-gradle-buil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!