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

正如前面提到的 给你,Android M 不支持 Apache HTTP API:

而是使用 HttpURLConnection 类。

或者

要继续使用 Apache HTTP API,必须首先在 build.gradle 文件中声明以下编译时依赖项:

Android { useLibrary‘ org.apache.http.Heritage’ }

我已经将我的项目对 HttpClient 的大部分使用转换成了 HttpURLConnection,但是,我仍然使用 需要在一些领域中使用 HttpClient。因此,我尝试将‘ org.apache.http.Heritage’声明为编译时依赖项,但是在 build.gradle 中得到了一个错误:

未找到分级 DSL 方法: ‘ useLibrary ()’

我的问题是: 如何在项目中声明‘ org.apache.http.Heritage’作为编译时依赖项?

非常感谢你的帮助,谢谢

109056 次浏览

To resolve the issues make sure you are using build tools version "23.0.0 rc2" with the following tools build gradle dependency:

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

I solved this problem like so:

1.) Set classpath in top-level build file as GUG mentioned:

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

2.) In build file of specific module:

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

Another alternative is to just add jbundle dependency. This is more Android Studio friendly as Android Studio doesn't give the message "cannot resolve symbol..."

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

For API 23:

Top level build.gradle - /build.gradle

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

Module specific build.gradle - /app/build.gradle

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

Official docs (for preview though): http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

Latest android gradle plugin changelog: http://tools.android.com/tech-docs/new-build-system

In your build.gradle file add useLibrary 'org.apache.http.legacy' as per Android 6.0 Changes > Apache HTTP Client Removal notes.

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

To avoid missing link errors add to dependencies

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

using 'provided' the dependency will be not included in the apk

FWIW the removal of Apache library was foreshadowed a while ago. Our good friend Jesse Wilson gave us a clue back in 2011: http://android-developers.blogspot.com/2011/09/androids-http-clients.html

Google stopped working on ApacheHTTPClient a while ago, so any library that is still relying upon that should be put onto the list of deprecated libraries unless the maintainers update their code.

<rant> I can't tell you how many technical arguments I've had with people who insisted on sticking with Apache HTTP client. There are some major apps that are going to break because management at my not-to-be-named previous employers didn't listen to their top engineers or knew what they were talking about when they ignored the warning ... but, water under the bridge.

I win.

</rant>

it should help:

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

To avoid missing link errors add to dependencies

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

or

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

because

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

Just copied file: org.apache.http.legacy.jar from Android/Sdk/platforms/android-23/optional folder into project folder app/libs.

Worked like charm for 23.1.1.

As the answers are a bit old, I will put my solution (what worked for me), it can be helpful for somebody else... I took my solution from the official documentation of Apache, no work-around.

1/ in gradle:

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

2/ in the rest of the app replace the org.apache.http by cz.msebera.android.httpclient and all your imports (dependencies) will be fixed. you can just do ctrl+shift+R and replace it in the whole project.

Note for Android 9 (Pie).

Additionally to useLibrary 'org.apache.http.legacy' you have to add in AndroidManifest.xml:

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

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

Apache HTTP client deprecated

With Android 6.0, google removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default.

To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their

AndroidManifest.xml:

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

Note:

The android:required="false" attribute is required for apps that have a minimum SDK of 23 or lower, because on devices with API levels lower than 24, the org.apache.http.legacy library is not available. (On those devices, the Apache HTTP classes are available on the bootclasspath.)

Found All android 9.0 changes :

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