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,然后编译失败?

4

6 回答 6

79

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

尝试添加此依赖项:

compile "org.apache.httpcomponents:httpmime:4.2.3"
于 2013-04-03T10:03:26.820 回答
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'
}
于 2015-03-13T12:52:19.420 回答
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(都是相同的版本)

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

于 2015-09-12T22:54:12.527 回答
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'
    }
}
于 2015-12-08T10:05:46.493 回答
2

因为官方的 Android API 包含 httpclient,我们删除了对 httpclient 的所有依赖,包括它的传递依赖。

如果你真的想使用 httpclient,我会用 jarjar 重新打包它,重命名包并改用它。

至于 httpmime,它看起来实际上不在 android.jar 中,因此我们可以避免将其过滤掉,但现在您必须手动添加它。

我们可能希望在构建系统进入 1.0 之前对其进行调整

于 2013-04-03T18:01:55.387 回答
0

只需将其添加到 build.gradle(Module: app) 文件中:

dependencies {
...
   implementation "org.apache.httpcomponents:httpmime:4.5.6"
}
于 2019-05-03T07:08:24.310 回答