How to add Apache HTTP API (legacy) as compile-time dependency to build.grade for Android M?
Solution 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
Solution 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'
}
Solution 3
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
Solution 4
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
Solution 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.
Comments
-
Virat Singh over 2 years
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
-
Hesham Fas over 8 yearsit's available on the mavenCentral() repository.
-
Yuriy Chernyshov over 8 yearsIt can not be compiled: Error:Execution failed for task ‘:<Proj>:package<Proj>Debug'. > Unable to compute hash of /Users/<user>/Documents/<Proj>/<Activity>/build/intermediates/classes-proguard/<Proj>/debug/classes.jar
-
Hesham Fas over 8 yearshave you tried compiling (rebuild) without proguard?
-
android developer over 8 yearscan you please show a sample project that works with this ? Now I'm getting this error: Error:Execution failed for task ':app:packageAllSyncmeappDebugClassesForMultiDex'. > java.util.zip.ZipException: duplicate entry: org/apache/http/annotation/GuardedBy.class
-
Hesham Fas over 8 yearsWell, The type of error you have seems specific to your project. You have duplicate apache packages. One apparently in the org.jbundle. .... and the other somewhere in one of your Libraries. I would look for the duplicate package in:
-
Hesham Fas over 8 years1. the project target sdk is lower than 23 therfore the apache package is still in the android.jar 2. There is a jar file that contains same package included in the JBundle 3. you've used both solutions the solution suggested by android and my alternative solution. good luck
-
Varnit Khandelwal over 8 yearsI update gradle build version and then declare useLibrary for apache also but then also I will getting error: Error:(204, 13) error: cannot find symbol class DefaultHttpClient Error:(204, 48) error: cannot find symbol class DefaultHttpClient Error:(205, 13) error: cannot find symbol class HttpPost Error:(205, 37) error: cannot find symbol class HttpPost Error:(207, 13) error: cannot find symbol class HttpResponse Error:(208, 13) error: cannot find symbol class HttpEntity Error:(209, 19) error: cannot find symbol variable EntityUtils
-
Graeme over 8 yearsSomething I missed from the answer: You need to ensure that the gradle classpath is in your apps top level build file, where the
useLibrary
must be in your app specific build file. -
brockoli over 8 yearsThis is the only solution I could get to work. For some reason useLibrary didn't work for me even though I'm using 1.3.1 of the gradle plugin. But compiling the http client lib in dependencies worked.
-
Sheraz Ahmad Khilji over 8 yearsAlso dont forget to add this jar file in order to get the above solution working :)
-
Joe Bowbeer over 8 yearsMy understanding is that the Apache HttpClient is hidden in android-23 but is not actually removed. Removal would break lots of existing clients targeting earlier platforms, where HttpClient is not hidden, and these existing apps are still expected to run on android-23. On android-23, adding useLibrary serves to append these legacy classes to the boot classpath, that is, to the list of classes that are supplied by the platform. This essentially unhides the classes on android-23.
-
Admin over 8 yearsor give compile instead of provided both will be work
-
Richard Le Mesurier over 8 years@Sheraz that is no longer required - the gradle build picks it up automatically
-
Sindri Þór over 8 yearsFinally, worked for me using compileSdkVersion 23 buildToolsVersion '22.0.0'
-
RoFF about 8 yearswith above config, I met with Error:(155, 0) Gradle DSL method not found: 'provided()'
-
Coder Roadie about 8 yearsWhen an class is deprecated it's common to first hide it to prevent it from being used going forward. Then it can be removed as time goes on. Something like: 1. mark as deprecated in the current version. 2. hide in the second version. 3. refactor all code to eliminate the old class completely.
-
Ratul over 7 yearsMy gradle version 2.2.0, used
useLibrary 'org.apache.http.legacy'
. I also havecompile('org.apache.httpcomponents:httpmime:4.3') { exclude group: 'org.apache.httpcomponents', module: "httpclient" }
- these lines included. Then while building this error occursExecution failed for task ':transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE File1: ..\httpclient-android-4.3.5.1.jar File2: ..\httpmime-4.3.jar
-
Ratul over 7 yearsCan you please suggest me how to solve this? (sorry for the double comment)
-
hidro over 7 years@Ratul you should add a
packagingOptions {}
block insideandroid
block, inside this block, addexclude 'META-INF/LICENSE'
... (each line per duplicate file reported) -
lorenzo over 5 yearsWhich version of the Apache HTTP library does this use?