如何在Android中使用gradle构建文件正确导入org.apache的HttpClient?

38

尝试运行“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;
                              ^

我的 build.gradle 的结尾看起来像这样:

    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"
    } 
}
为什么编译过程会忽略HttpClient,然后无法编译?
6个回答

79
我认为httpclient库中不包括mime部分,这些部分在httpmime中。这是httpclient的传递依赖项,但由于被忽略,它不会被考虑在内。
尝试添加此依赖项:
compile "org.apache.httpcomponents:httpmime:4.2.3"

2
httpmime 4.2+ 的一部分依赖于 org.apache.http.entity.ContentType,该组件存在于 httpcore 4.2+ 中,但在 android.jar(至少针对 API 9...19)中不存在。因此,您可能希望限制自己依赖于 org.apache.httpcomponents:httpmime:4.1.3,以确保正常运行。 - averasko
从谷歌上看,它说传递依赖现在已经解决。自Gradle插件的1.0版本以来,我们已经升级到支持高级自定义依赖项解析的Gradle 2.2+。这将允许您使用不同的依赖项覆盖任何传递依赖项。这应该可以帮助您解决依赖于损坏的依赖项的问题。 - anshad
1
我无法使用MultipartEntityBuilder,有什么替代方案吗?已经寻找解决方案两天了... - Jimit Patel
2
@JimitPatel - 在你的 gradle 中添加以下内容 compile ('org.apache.httpcomponents:httpmime:4.3.5') { exclude group: 'org.apache.httpcomponents', module: 'httpclient' } - pRaNaY
我找到了解决方案... 这是我的版本 - https://dev59.com/SI_ea4cB1Zd3GeqPPIKo - Jimit Patel

17

http-mime作为依赖项添加会导致httpclient被作为传递性依赖项包含在内,这导致我遇到了与OP相同的警告。 我不得不告诉gradle忽略传递性依赖关系:

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

救命稻草!这正是我正在寻找的!谢谢! - Tino

13

对于Android系统,现在有HttpClient 4.3.X重新打包的Maven分发版本可用。

项目仓库:https://github.com/smarek/httpclient-android
Maven标签:cz.msebera.android:httpclient:4.3.+
发布到了Maven中央仓库。

该版本(4.3.3)包括HttpCore、HttpClient、HttpClient-Cache和HttpMime(所有版本都相同)。

声明:我是该项目的作者。


我使用了这个包来帮助我处理AsyncHttp库。 - Zapnologica
你救了我的一天。 - Toan Tran Van

3

补充一下,我通过使用这个方法解决了问题,如果你的compileSdkVersion是19(在我的情况下)

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'

如果您的compileSdkVersion是23,则使用以下代码:

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

2
因为官方Android API包含httpclient,所以我们移除了所有与httpclient相关的依赖,包括其传递依赖。
如果您确实想使用httpclient,我建议您使用jarjar重新打包,重命名包并使用这个版本。
至于httpmime,看起来它实际上不在android.jar中,所以我们可以避免将其过滤掉,但现在您需要手动添加它。
在构建系统达到1.0之前,我们可能需要进行一些微调。

1
我也遇到了这个问题。你的回答有点令人困惑。你是说Android Studio附带的Gradle版本会强制执行这种行为吗?除了使用Jarjar之外,没有其他方法可以解决吗? - Amaron
1
我也遇到了这个问题。如何在不引入httpmime并陷入混乱的情况下包含多部分上传支持? - Greg Ennis

0

只需将以下内容添加到 build.gradle(Module: app) 文件中:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接