如何正确地从Android上使用Gradle构build文件从org.apache导入HttpClient?
我看到这个错误,当我尝试运行“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,但是不能编译?
我认为httpclient库不包括MIME部分,那些在httpmime中。 这是httpclient的传递依赖关系,但是由于被忽略,所以不会被考虑在内。
尝试添加这个依赖:
compile "org.apache.httpcomponents:httpmime:4.2.3"
将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' }
对于Android,现在有可用的HttpClient 4.3.X重新打包的Maven发行版
项目回购: https : //github.com/smarek/httpclient-android
Maven标签: cz.msebera.android:httpclient:4.3.+
: cz.msebera.android:httpclient:4.3.+
: cz.msebera.android:httpclient:4.3.+
发布到Maven中央存储库
在4.3.3版本中包括HttpCore,HttpClient,HttpClient-Cache和HttpMime(都是同一版本)
免责声明:我是该项目的作者
除此之外,我解决了这个问题,如果你的compileSdkVersion是19(IN MY CASE)
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' } }
由于官方Android API包含httpclient,我们删除了对httpclient的所有依赖,包括它的传递依赖。
如果你真的想使用httpclient,我会用jarjar重新打包,重命名包,并使用它。
至于httpmime,看起来它不是真的在android.jar中,所以我们可以避免过滤它,但现在你必须手动添加它。
在构build系统变为1.0之前,我们可能需要调整它