java Apache Fluent客户端对HttpClient的可传递依赖性导致NoClassDefFoundException
我有以下gradle文件:
apply plugin: 'com.安卓.application'
安卓 {
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-安卓.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.安卓.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' }
格雷德尔的警告确实已经消失,但问题依然存在:
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'
我怎样才能上班?既然根据51.2.3 Resolve version conflicts这是默认的解决策略,为什么它不直接选择最新版本的httpclient包呢
# 1 楼答案
因为Android附带了一个非常过时(BETA1之前)的HttpClient 4.0版本,这使得它不可能使用任何版本的股票HttpClient。Gradle插件显然足够聪明,可以警告您不兼容和可能的冲突
这里有两个选项:
使用官方的Android port of HttpClient,默认情况下,该API与Android附带的版本兼容
使用Dirk Boye开发的scripts将HttpClient重新打包到不同的名称空间。您使用最新版本的HttpClient的里程可能会有所不同
# 2 楼答案
似乎在使用Android Studio 1.4构建API 23时,这行代码会导致“无法解决”错误:
改用这个:
在这里下载jar:http://mvnrepository.com/artifact/cz.msebera.android/httpclient/4.4.1.1