如何将 Apache HTTP API(旧版)作为编译时依赖项添加到 build.grade for Android M?

How to add Apache HTTP API (legacy) as compile-time dependency to build.grade for Android M?

如前所述here,Android M 将不支持 Apache HTTP API。文档说明:

use the HttpURLConnection class instead.

To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

android { useLibrary 'org.apache.http.legacy' }

我已经将我的项目对 HttpClient 的大部分使用转换为 HttpURLConnection,但是,我仍然需要在一些区域使用 HttpClient。因此,我试图将 'org.apache.http.legacy' 声明为编译时依赖项,但在 build.gradle:

中出现错误

Gradle DSL method not found: 'useLibrary()'

我的问题是:如何在我的项目中将 'org.apache.http.legacy' 声明为编译时依赖项?

非常感谢任何帮助。谢谢

要解决这些问题,请确保您使用的构建工具版本“23.0.0 rc2”具有以下工具构建 gradle 依赖项:

classpath 'com.android.tools.build:gradle:1.3.0-beta2'

我是这样解决这个问题的:

1.) 如 GUG 所述,在顶级构建文件中设置类路径:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0-beta2'
    }
    allprojects {
        repositories {
           jcenter()
        }
    }
}

2.) 在特定模块的构建文件中:

android {
   useLibrary 'org.apache.http.legacy'
   compileSdkVersion 'android-MNC'
   buildToolsVersion '23.0.0 rc3'
}

另一种选择是只添加 jbundle 依赖项。 这更 Android Studio 友好,因为 Android Studio 没有给出消息 "cannot resolve symbol..."

 dependencies {
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
 }

对于 API 23:

顶层build.gradle - /build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}
...

模块特定 build.gradle - /app/build.gradle

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
    useLibrary 'org.apache.http.legacy'
    ...
}

官方文档(虽然用于预览):http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

最新 android gradle 插件更新日志:http://tools.android.com/tech-docs/new-build-system

在您的 build.gradle 文件中,根据 Android 6.0 Changes > Apache HTTP Client Removal 注释添加 useLibrary 'org.apache.http.legacy'

android {
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}

为避免遗漏 link 错误添加到依赖项

dependencies {
    provided 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

使用 'provided' 依赖项将不会包含在 apk 中

FWIW 删除 Apache 库是前一段时间预示的。我们的好朋友 Jesse Wilson 在 2011 年给了我们一条线索: http://android-developers.blogspot.com/2011/09/androids-http-clients.html

Google 不久前停止在 ApacheHTTPClient 上工作,所以任何仍然依赖它的库都应该被放入弃用库列表,除非维护者更新他们的代码。

<rant> 我无法告诉您我与坚持使用 Apache HTTP 客户端的人有多少技术争论。有一些主要的应用程序将要崩溃,因为我不愿透露姓名的前雇主的管理层没有听取他们的顶级工程师的意见,或者在他们忽略警告时不知道他们在说什么......但是,水下桥.

我赢了

</rant>

应该有帮助:

android {
    ...
    useLibrary 'org.apache.http.legacy'
    ...
}

为避免遗漏link错误添加到依赖项

dependencies {
    provided 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

dependencies {
    compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

因为

Warning: Configuration 'provided' is obsolete and has been replaced with 'compileOnly'.

刚刚将文件:org.apache.http.legacy.jarAndroid/Sdk/platforms/android-23/optional 文件夹复制到项目文件夹 app/libs

为 23.1.1 工作得很好。

由于答案有点旧,我会提出我的解决方案(对我有用的),它可能对其他人有帮助...我的解决方案来自 Apache 的 official documentation,没有解决方法。

1/ 在 gradle:

dependencies {
...
// This is the maintained version from apache.
compile group: 'cz.msebera.android', name: 'httpclient', version: '4.4.1.1'
}

2/ 在应用程序的其余部分将 org.apache.http 替换为 cz.msebera.android.httpclient,您的所有导入(依赖项)都将得到修复。你可以只做 ctrl+shift+R 并在整个项目中替换它。

Android9(饼图)的注意事项。

useLibrary 'org.apache.http.legacy' 外,您还必须添加 AndroidManifest.xml:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

来源:https://developer.android.com/about/versions/pie/android-9.0-changes-28

Apache HTTP 客户端已弃用

在 Android 6.0 中,google 删除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库已从引导类路径中删除,默认情况下对应用程序不可用。

要继续使用 Apache HTTP 客户端,面向 Android 9 及更高版本的应用可以将以下内容添加到

AndroidManifest.xml:

<uses-library android:name="org.apache.http.legacy" android:required="false"/>

注:

最小 SDK 为 23 或更低的应用需要 android:required="false" 属性,因为在 API 级别低于 24 的设备上,org.apache.http.legacy图书馆不可用。 (在这些设备上,Apache HTTP 类 在引导类路径上可用。)

找到所有 android 9.0 更改:

https://developer.android.com/about/versions/pie/android-9.0-changes-28