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

xrd*_*xrd 38 android gradle apache-httpclient-4.x

当我尝试运行"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;
                              ^
Run Code Online (Sandbox Code Playgroud)

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"
    } 
}
Run Code Online (Sandbox Code Playgroud)

为什么编译过程会忽略HttpClient,但是无法编译?

Hie*_*mus 79

我认为httpclient库不包含mime部分,它们都在httpmime中.这是httpclient的传递依赖,但由于忽略了它,因此不会将其考虑在内.

尝试添加此依赖项:

compile "org.apache.httpcomponents:httpmime:4.2.3"
Run Code Online (Sandbox Code Playgroud)

  • httpmime 4.2+的一部分依赖于httpd 4.2+中存在但未出现在android.jar中的org.apache.http.entity.ContentType(至少对于API 9 ... 19而言).因此,您可能希望限制自己依赖于org.apache.httpcomponents:httpmime:4.1.3没问题. (2认同)
  • @JimitPatel - 在你的`gradle``compring中添加以下内容('org.apache.httpcomponents:httpmime:4.3.5'){exclude group:'org.apache.httpcomponents',module:'httpclient'}` (2认同)

twe*_*e17 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'
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*era 13

对于Android,现在有可用的HttpClient 4.3.X重新打包的Maven发行版

项目回购:https://github.com/smarek/httpclient-android
Maven标签:cz.msebera.android:httpclient:4.3.+
发布到Maven Central存储库

在4.3.3版本中包括HttpCore,HttpClient,HttpClient-Cache和HttpMime(所有版本相同)

免责声明:我是该项目的作者