Apache Fluent Client 对 HttpClient 的传递依赖导致 NoClassDefFoundExceptions

Apache Fluent Client's transitive dependency on HttpClient causes NoClassDefFoundExceptions

我有以下 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
    }

    defaultConfig {
        applicationId "com.guesstheurf.guesstheurf"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile (
            [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.5.0'],
            [group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.5.0'],
            [group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.5.0']
    )

    compile ('org.apache.httpcomponents:fluent-hc:4.4.1')
}

当我构建这个时,我收到警告说

Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for release as it may be conflicting with the internal version provided by Android.

这是httpcomponents:fluent-hc依赖的依赖。

我在依赖管理上找到了这个来源(chapter 51.4.7: Excluding transitive dependencies) 这表明我可以排除导致冲突的传递依赖。

然而当我把它改成

compile ('org.apache.httpcomponents:fluent-hc:4.4.1') { exclude module: 'httpclient' }

Gradle警告确实消失了,但问题仍然存在:

Caused by: java.lang.NoClassDefFoundError: org.apache.http.client.fluent.InternalEntityEnclosingHttpRequest at org.apache.http.client.fluent.Request.Post(Request.java:95)

当我使用

时问题仍然存在
exclude group: 'org.apache.httpcomponents', module: 'httpclient'

this answer.

所示

当我使用

configurations {
    all*.exclude module: 'httpclient'
}

警告也消失了,但运行时错误仍然存​​在。

This source 建议使用 force = true 强制我自己的版本也不能解决问题。

显式添加与 httpclient 相关的依赖项 as suggested here 也不起作用(并且不会删除警告):

compile 'org.apache.httpcomponents:httpclient:4.4.1'
compile 'org.apache.httpcomponents:fluent-hc:4.4.1'
compile "org.apache.httpcomponents:httpmime:4.4.1"
compile 'org.apache.httpcomponents:httpcore:4.4.1'

我怎样才能让 fluent-hc 工作?为什么它不只是 select 最新版本的 httpclient 包,因为这是根据 51.2.3 Resolve version conflicts?

的默认解析策略

And why doesn't it simply select the newest version of the httpclient package since that is the default resolution strategy according

因为 Android 附带了一个非常过时(BETA1 之前)的 HttpClient 4.0 版本,这使得无法使用任何版本的现有 HttpClient。 Gradle 插件显然足够聪明,可以警告您不兼容和可能的冲突。

这里有两个选择:

  1. 使用官方Android port of HttpClient,默认API兼容Android附带的版本

    dependencies {
        compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
        compile group: 'org.apache.httpcomponents' , name: 'fluent-hc' , version: '4.3.5'
    }
    
  2. 使用 Dirk Boye 开发的 scripts 将 HttpClient 重新打包成不同的名称 space。您使用最新版本的 HttpClient 的情况可能会有所不同。

    org.apache.http -> thank.you.google.org.apache.http
    

似乎在使用 Android Studio 1.4 构建 API 23 时,此行导致 "Failed to resolve" 错误:

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'

改用这个:

compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'

在此处下载 jar:http://mvnrepository.com/artifact/cz.msebera.android/httpclient/4.4.1.1