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

非 Y 不嫁゛ 提交于 2019-11-28 06:44:29

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"

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

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

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

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

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

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