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

11
  • 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. Commented Jun 15, 2015 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? Commented Jun 15, 2015 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. Commented Jun 16, 2015 at 0:00
  • I just tried '1.2.3' and no luck - same error -> "Gradle DSL method not found: 'useLibrary()'" :/ Commented Jun 16, 2015 at 0: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). Commented Jun 16, 2015 at 0:08

11 Answers 11

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

Sign up to request clarification or add additional context in comments.

8 Comments

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
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.
Also dont forget to add this jar file in order to get the above solution working :)
@Sheraz that is no longer required - the gradle build picks it up automatically
@Ratul you should add a packagingOptions {} block inside android block, inside this block, add exclude 'META-INF/LICENSE'... (each line per duplicate file reported)
|
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'
 }

8 Comments

it's available on the mavenCentral() repository.
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/intermediates/classes-proguard/<Proj>/debug/classes.jar
have you tried compiling (rebuild) without proguard?
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
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:
|
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

Comments

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

Comments

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.

Comments

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'.

2 Comments

or give compile instead of provided both will be work
with above config, I met with Error:(155, 0) Gradle DSL method not found: 'provided()'
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'
}

Comments

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.

Comments

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>

2 Comments

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.
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.
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

Comments

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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.