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

106,371

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.

Share:
106,371
Virat Singh
Author by

Virat Singh

android developer

Updated on September 01, 2021

Comments

  • Virat Singh
    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
    Hesham Fas over 8 years
    it's available on the mavenCentral() repository.
  • Yuriy Chernyshov
    Yuriy Chernyshov over 8 years
    It can not be compiled: Error:Execution failed for task ‘:<Proj>:package<Proj>Debug'. > Unable to compute hash of /Users/<user>/Documents/<Proj>/<Activity>/build/intermediate‌​s/classes-proguard/<‌​Proj>/debug/classes.‌​jar
  • Hesham Fas
    Hesham Fas over 8 years
    have you tried compiling (rebuild) without proguard?
  • android developer
    android developer over 8 years
    can 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
    Hesham Fas over 8 years
    Well, 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
    Hesham Fas over 8 years
    1. 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
    Varnit Khandelwal over 8 years
    I 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
    Graeme over 8 years
    Something 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
    brockoli over 8 years
    This 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
    Sheraz Ahmad Khilji over 8 years
    Also dont forget to add this jar file in order to get the above solution working :)
  • Joe Bowbeer
    Joe Bowbeer over 8 years
    My 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
    Admin over 8 years
    or give compile instead of provided both will be work
  • Richard Le Mesurier
    Richard Le Mesurier over 8 years
    @Sheraz that is no longer required - the gradle build picks it up automatically
  • Sindri Þór
    Sindri Þór over 8 years
    Finally, worked for me using compileSdkVersion 23 buildToolsVersion '22.0.0'
  • RoFF
    RoFF about 8 years
    with above config, I met with Error:(155, 0) Gradle DSL method not found: 'provided()'
  • Coder Roadie
    Coder Roadie about 8 years
    When 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
    Ratul over 7 years
    My gradle version 2.2.0, used useLibrary 'org.apache.http.legacy'. I also have compile('org.apache.httpcomponents:httpmime:4.3') { exclude group: 'org.apache.httpcomponents', module: "httpclient" } - these lines included. Then while building this error occurs Execution 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
    Ratul over 7 years
    Can you please suggest me how to solve this? (sorry for the double comment)
  • hidro
    hidro over 7 years
    @Ratul you should add a packagingOptions {} block inside android block, inside this block, add exclude 'META-INF/LICENSE'... (each line per duplicate file reported)
  • lorenzo
    lorenzo over 5 years
    Which version of the Apache HTTP library does this use?