99

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

Community
  • 1
  • 1
Virat Singh
  • 1,464
  • 1
  • 12
  • 16
  • 3
    Make sure that you are using a fairly recent Gradle for Android plugin. My guess is that this is really new, meaning you would need something like `1.3.0-rc2`. You might also consider using [Apache's own Android-compatible edition of HttpClient](https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html). – CommonsWare Jun 15 '15 at 23:53
  • Thanks for the quick response @CommonsWare ... Are you referring to the "classpath 'com.android.tools.build:gradle:1.0.0'" line in the top-level build.gradle file? – Virat Singh Jun 15 '15 at 23:59
  • 1
    Yes. I will be rather surprised if `1.0.0` has the `useLibrary` thing. It's possible that it crept in before 1.3.x, so you could try `1.2.3` (AFAIK, the latest production release) and see what happens. – CommonsWare Jun 16 '15 at 00:00
  • I just tried '1.2.3' and no luck - same error -> "Gradle DSL method not found: 'useLibrary()'" :/ – Virat Singh Jun 16 '15 at 00:04
  • Yeah, since this is tied to the M Developer Preview, I am not shocked by that. Probably you need `1.3.0-rc2` (or something newer, if there is one). – CommonsWare Jun 16 '15 at 00:08
  • Maybe try with `1.3.0-beta3` (from http://tools.android.com/tech-docs/new-build-system). My build still fails with that but it goes one step further. – mbonnin Jun 18 '15 at 22:24
  • compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5' try that in dependencies and change the nameSpace over to apache "HC4" prefixes like org.apache.http.client.methods.{$Rootname}HC4; – Robert Rowntree Jun 21 '15 at 15:07
  • How exactly should I add the dependency? Should I just add the `useLibrary 'org.apache.http.legacy'` line below `compileSdkVersion 'android-MNC'`? Or should I somehow put it between `dependencies {...}` – Pitel Jul 13 '15 at 13:02
  • adding useLibrary does nothing for me – lxknvlk Sep 07 '15 at 21:53
  • To clarify what useLibrary is doing: my understanding is that the Apache HttpClient is *hidden* in android-23 but is not actually removed. Adding useLibrary serves to add these legacy classes to the boot classpath, essentially unhiding these classes at runtime. – Joe Bowbeer Oct 19 '15 at 03:15
  • Possible duplicate of [HttpEntity is deprecated on Android now, what's the alternative?](http://stackoverflow.com/questions/29150184/httpentity-is-deprecated-on-android-now-whats-the-alternative) – C0D3LIC1OU5 Jan 13 '16 at 22:37

11 Answers11

173

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

vitriolix
  • 351
  • 3
  • 9
hidro
  • 12,333
  • 6
  • 53
  • 53
  • 2
    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 – Varnit Khandelwal Sep 08 '15 at 04:55
  • 1
    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. – Graeme Sep 08 '15 at 10:04
  • Also dont forget to add this [jar](http://stackoverflow.com/a/32328502/1118886) file in order to get the above solution working :) – Sheraz Ahmad Khilji Oct 10 '15 at 21:00
  • @Sheraz that is no longer required - the gradle build picks it up automatically – Richard Le Mesurier Nov 23 '15 at 12:58
  • 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 Oct 05 '16 at 10:37
  • Can you please suggest me how to solve this? (sorry for the double comment) – Ratul Oct 05 '16 at 10:38
  • 1
    @Ratul you should add a `packagingOptions {}` block inside `android` block, inside this block, add `exclude 'META-INF/LICENSE'`... (each line per duplicate file reported) – hidro Oct 05 '16 at 13:36
  • Which version of the Apache HTTP library does this use? – lorenzo Nov 19 '18 at 16:12
30

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'
 }
ByteWelder
  • 5,464
  • 1
  • 38
  • 45
Hesham Fas
  • 876
  • 1
  • 9
  • 20
  • it's available on the mavenCentral() repository. – Hesham Fas Aug 27 '15 at 19:54
  • It can not be compiled: Error:Execution failed for task ‘::packageDebug'. > Unable to compute hash of /Users//Documents///build/intermediates/classes-proguard//debug/classes.jar – Yuriy Chernyshov Aug 29 '15 at 00:01
  • have you tried compiling (rebuild) without proguard? – Hesham Fas Aug 29 '15 at 13:24
  • 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 – android developer Sep 03 '15 at 09:13
  • 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 Sep 06 '15 at 02:58
  • 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 – Hesham Fas Sep 06 '15 at 03:07
  • 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. – brockoli Sep 21 '15 at 17:40
  • Finally, worked for me using compileSdkVersion 23 buildToolsVersion '22.0.0' – Sindri Þór Jan 21 '16 at 17:53
16

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

Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
15

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

Dan Dar3
  • 1,199
  • 13
  • 23
lrampazzo
  • 1,219
  • 9
  • 7
11

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
CodeBulls Inc.
  • 462
  • 3
  • 11
3

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'.
guenis
  • 2,520
  • 2
  • 25
  • 37
2

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'
}
A.D.
  • 1,412
  • 2
  • 19
  • 37
2

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.

ahmed_khan_89
  • 2,755
  • 26
  • 49
1

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>

Coder Roadie
  • 838
  • 1
  • 8
  • 11
  • 1
    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. – Joe Bowbeer Oct 19 '15 at 03:14
  • 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. – Coder Roadie Apr 12 '16 at 23:48
1

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

Islam Alshnawey
  • 692
  • 10
  • 15
0

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'
GUG
  • 221
  • 2
  • 3