问题
As mentioned here, Android M will not support the Apache HTTP API. The docs state to:
use the HttpURLConnection class instead.
or
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\' }
I have converted much of my project\'s usage of HttpClient to HttpURLConnection, however, I still need to use the HttpClient in a few areas. Hence, I am trying to declare \'org.apache.http.legacy\' as a compile-time dependency but am getting an error in build.gradle:
Gradle DSL method not found: \'useLibrary()\'
My question is: how do I declare \'org.apache.http.legacy\' as a compile-time dependency in my project?
Any help is much appreciated. Thanks
回答1:
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
回答2:
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'
}
回答3:
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
回答4:
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
回答5:
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.
回答6:
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.
回答7:
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'
}
回答8:
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>
回答9:
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'.
回答10:
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'
来源:https://stackoverflow.com/questions/30856785/how-to-add-apache-http-api-legacy-as-compile-time-dependency-to-build-grade-fo