如何正确地从org.apache使用gradle这个build文件导入的HttpClient在And

2019-08-19 03:39发布

当我尝试运行“gradle这个建”我看到这个错误

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,但随后未能编译?

Answer 1:

我认为HttpClient库不包含MIME部分,这些都是在httpmime。 这是HttpClient的一个传递依赖,但是作为被忽略,它不会被考虑。

尝试添加这种依赖性:

compile "org.apache.httpcomponents:httpmime:4.2.3"


Answer 2:

添加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'
}


Answer 3:

对于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(所有版本相同)

免责声明:上述项目我作家



Answer 4:

添加到这一点,我利用这一点,如果你的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'
    }
}


Answer 5:

由于官方Android的API包括我们的HttpClient上删除所有的HttpClient依赖,包括它的传递依赖。

如果你真的想用HttpClient的,我会用jarjar重新包装,重新命名的包,并使用这个代替。

至于httpmime,它看起来像它不是实际的android.jar所以我们可以避开过滤出来,但现在你必须手动添加。

我们可能要调整这个构建系统进入前1.0



Answer 6:

只需添加这的build.gradle(模块:应用程序)文件:

dependencies {
...
   implementation "org.apache.httpcomponents:httpmime:4.5.6"
}


文章来源: How do I properly import HttpClient from org.apache on Android using gradle build file?