Android Gradle 依赖

Android Gradle Dependency

我正在尝试在我的 cordova 项目中添加以下依赖项。然而,依赖在构建过程中被忽略了。

compile 'org.apache.httpcomponents:httpclient:4.5.2'

从类似问题的 Whosebug 回答中,我意识到我必须排除一些模块和组。

所以我对依赖项进行了以下更改。

compile 'org.apache.httpcomponents:httpclient:4.5.2'
compile ('org.apache.httpcomponents:httpclient:4.5.2'){
    exclude module: 'httpclient' //by artifact name
    exclude group: 'org.apache.httpcomponents' //by group
    exclude group: 'org.apache.httpcomponents', module: 'httpclient' //by both name and group
} 

但是,我仍然收到此警告。

WARNING: Dependency org.apache.httpcomponents:httpclient:4.5.2 is ignored for debug as it 
 may be conflicting with the internal version provided by Android.
 In case of problem, please repackage it with jarjar to change the class packages.

因此,我所有的 HTTP 命令,如 HTTP Post、HTTP 客户端等都没有解析。

有人可以告诉我究竟需要排除什么以及是否需要进行任何配置更改吗?

使用适合各自 android 版本的 Apache http 客户端的重新打包版本。

对于 Android API 22 岁及以上

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

对于 Android API 23 和更新

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

这个 link 有更多信息 - https://hc.apache.org/httpcomponents-client-4.5.x/android-port.html

您可以通过添加此行来使用 HTTP 库来构建您的应用脚本:

useLibrary 'org.apache.http.legacy'

喜欢:

android {
    compileSdkVersion XX
    buildToolsVersion "XX.X.X"

    useLibrary 'org.apache.http.legacy'

    // other lines
}

希望对您有所帮助:)