Android Gradle Apache HttpClient does not exist?

231,304

Solution 1

I suggest you replace the deprecated apache HttpClient with the new HttpURLConnection.

That's a cleaner solution, it's quite easy to migrate, and generally it's better to stick to the latest SDK changes than trying to hack/patch/workaround: you usually regret it later :)

Step 1

HttpGet httpGet = new HttpGet(url);

becomes:

URL urlObj = new URL(url);

Step 2

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(httpGet, localContext);
InputStream is = response.getEntity().getContent();

becomes:

HttpURLConnection urlConnection = (HttpURLConnection) urlObj.openConnection();
InputStream is = urlConnection.getInputStream();

Step 2 bis

int status = response.getStatusLine().getStatusCode();

becomes:

int status = urlConnection.getResponseCode();

Solution 2

if you are using target sdk as 23 add below code in your build.gradle

android{
    compileSdkVersion 23
    buildToolsVersion '23.0.1'
    useLibrary  'org.apache.http.legacy'
}

and change your buildscript to

classpath 'com.android.tools.build:gradle:1.3.0' 

for more info follow this link

Solution 3

I had this problem and then found these pages: Here you can see that apache library is deprecated, but it's not removed, so it should work. It doesn't.

See.

And here you can see how to include apache library to your project

See.

I resolved problem by adding following to my build.gradle file as recommended in second link.

android {
    useLibrary 'org.apache.http.legacy'
}

However this only works if you are using gradle 1.3.0-beta2 or greater, so you will have to add this to buildscript dependencies if you are on a lower version:

classpath 'com.android.tools.build:gradle:1.3.0-beta2'

Hope this helps.

Solution 4

Add this library into build.gradle

 android {
      useLibrary 'org.apache.http.legacy'
 }

Solution 5

copy org.apache.http.legacy.jar which is in Android/Sdk/platforms/android-23/optional folder to to app/libs

and also added this line to app.gradle file

compile files('libs/org.apache.http.legacy.jar')

But if you're using more jar libraries, you can use this way

compile fileTree(dir: 'libs', include: ['*.jar'])
Share:
231,304

Related videos on Youtube

Apqu
Author by

Apqu

Software developer based in Suffolk, UK. Specialising in: ASP.NET (C#) Xamarin Development HTML / CSS JavaScript / JQuery SQL Server 2000 - 2019

Updated on December 03, 2020

Comments

  • Apqu
    Apqu over 3 years

    I am trying to convert an IntelliJ project to the Gradle system of Android Studio but I am running into errors with Apache HttpClient? Am I missing something, the errors I am getting are as follows:

    Error:(10, 30) error: package org.apache.http.client does not exist
    Error:(11, 30) error: package org.apache.http.client does not exist
    Error:(12, 37) error: package org.apache.http.client.entity does not exist
    Error:(13, 38) error: package org.apache.http.client.methods does not exist
    Error:(14, 38) error: package org.apache.http.client.methods does not exist
    Error:(15, 38) error: package org.apache.http.client.methods does not exist
    Error:(16, 35) error: package org.apache.http.impl.client does not exist
    Error:(134, 33) error: cannot find symbol class HttpUriRequest
    Error:(164, 39) error: cannot find symbol class HttpUriRequest
    Error:(106, 17) error: cannot find symbol class HttpGet
    Error:(106, 39) error: cannot find symbol class HttpGet
    Error:(117, 17) error: cannot find symbol class HttpPost
    Error:(117, 40) error: cannot find symbol class HttpPost
    Error:(125, 43) error: cannot find symbol class UrlEncodedFormEntity
    Error:(135, 9) error: cannot find symbol class HttpClient
    Error:(135, 33) error: cannot find symbol class DefaultHttpClient
    Error:(155, 18) error: cannot find symbol class ClientProtocolException
    Error:(165, 9) error: cannot find symbol class HttpClient
    Error:(165, 33) error: cannot find symbol class DefaultHttpClient
    Error:(185, 18) error: cannot find symbol class ClientProtocolException
    

    My build.gradle file has the following dependencies:

    dependencies {
        compile 'com.google.android.gms:play-services:+'
        compile 'org.apache.httpcomponents:httpclient:4.2.6'
        compile 'org.apache.httpcomponents:httpmime:4.2.6'
        compile files('libs/core.jar')
    }
    

    It seems a lot of people are getting a similar problem but neither SO or Google have a solution so I am hoping this question will help future searchers.

  • Apqu
    Apqu almost 9 years
    Thanks for your answer, im afraid neither worked for me though :(
  • Daniel Nugent
    Daniel Nugent almost 9 years
    @tur that's strange, the second option worked for me. Make sure that your old jar files are deleted for the second option.
  • ssuukk
    ssuukk over 8 years
    For me it failed with: Error:Execution failed for task ':xenoAmp:mergeDebugJavaResources'. > Cannot determine expansion folder for L:\Projekty\xenoampgit\xenoAmp\build\intermediates\packagedJ‌​arsJavaResources\deb‌​ug\httpclient-androi‌​d-4.3.5.1.jar1037172‌​435\META-INF\LICENSE with folders L:\Projekty\xenoampgit\xenoAmp\build\intermediates\sourceFol‌​derJavaResources\deb‌​ug,L:\Projekty\xenoa‌​mpgit\xenoAmp\build\‌​intermediates\packag‌​edJarsJavaResources\‌​debug
  • Henrique de Sousa
    Henrique de Sousa over 8 years
    Supplied String module notation 'org.apache.http.legacy' is invalid.
  • Jinu
    Jinu over 8 years
    change your buildscript to classpath 'com.android.tools.build:gradle:1.3.0'
  • Henrique de Sousa
    Henrique de Sousa over 8 years
    Already had 1.3.1. Still cannot resolve classes inside Android Studio: Error:(14, 23) error: package org.apache.http does not exist
  • Jinu
    Jinu over 8 years
    is your targetSdkVersion 23 and build tools 23.0.0
  • Henrique de Sousa
    Henrique de Sousa over 8 years
    Yes, of course. Do your imports work fine? What Android Studio version are you using?
  • Jinu
    Jinu over 8 years
    android studio 1.4 beta 2
  • CeccoCQ
    CeccoCQ over 8 years
    @ssuukk Hi, did you find a solution for this error?
  • ssuukk
    ssuukk over 8 years
    Unfortunately the solution was to change target to current highest API, and that broke A LOT of things! Then I've had to add "useLibrary", "classpath" PLUS reference legacy apache lib, as described elsewhere in this question. And after it was compilable again I just decided to ditch Apache code altogether and replace it all with URLConnection, which was easier than I thought.
  • Silviu St
    Silviu St over 8 years
    Life saver response. Thanks!
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    What is libs/core.jar for?
  • russellhoff
    russellhoff over 8 years
    And what if you want to use the latest version of apache http components?
  • Jinu
    Jinu over 8 years
    @Silviu you can find dependency from here gradleplease.appspot.com/#httpmime
  • John81
    John81 over 8 years
    Agree with ssuukk. None of these answers worked for me so I ended up getting rid of the Apache libraries as well. I am pretty new to Android development but it was straightforward.
  • mithil1501
    mithil1501 over 8 years
    Thanx Jinu, updating Android Studio from 1.3 to above version is must. Got it working
  • Apqu
    Apqu over 8 years
    In another of my projects this proved to be the only answer that worked so +1 and thanks.
  • Alexiscanny
    Alexiscanny over 8 years
    I had the same issue and I fixed with useLibrary 'org.apache.http.legacy' how Milos said... however for @ssuukk with your issue you can try adding this always: android { packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/MANIFEST' }}
  • dibble
    dibble over 8 years
    why isnt this the answer?
  • Ivan Fazaniuk
    Ivan Fazaniuk over 8 years
    Same for me. Someone should accept this answer instead the one accepted now.
  • Ed Manners
    Ed Manners over 8 years
    This is the least path of resistance, not sure I want to down-grade gradle. It compile fine for me.
  • monkey0506
    monkey0506 over 8 years
    Thanks! This works with the gradle-experimental 0.6.0-alpha5 plugin. "useLibrary" doesn't seem to exist in the experimental plugin.
  • RoFF
    RoFF about 8 years
    it failed to resolve with error "Failed to resolve: org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.‌​org.apache.http.cli‌‌​​‌​ent:4.1.2". I am using experimental gradle 0.6.0-beta. can you figure it out ?
  • user961524
    user961524 about 8 years
    helped without disturbing the other settings. thanks
  • Bhushan Shirsath
    Bhushan Shirsath about 8 years
    i used this tutorial blazin.in/2016/03/http-connection-for-android-marshmallow.ht‌​ml it worked for me using HttpOpenUrlConnection clases
  • Naveed Ahmad
    Naveed Ahmad about 8 years
    Working, Thanks. 'org.apache.http.legacy' helped me, gradle was updated previously
  • mfaisalhyder
    mfaisalhyder almost 8 years
    helped in June 2016 :D (y) Eclipse still FTW :p
  • natsuki_2002
    natsuki_2002 almost 8 years
    Yea buddy! Thanks! great answer
  • maimoona
    maimoona over 7 years
    Great answer! Tried alot of other answers but changing one things broke other. This solution worked for me without change any existing configuration
  • Amrut Bidri
    Amrut Bidri over 7 years
    Worked for me .. Thanks a lot.
  • Dharma Sai Seerapu
    Dharma Sai Seerapu over 7 years
    Hey @Benjamin, how to send URL with PUT method?
  • Benjamin Piette
    Benjamin Piette over 7 years
    @TrickySolutions Sorry no idea, haven't used the PUT method yet.
  • Dan
    Dan almost 7 years
    can someone explain this? Why does this make it work? Why do we need to add this line, aside from adding the dependencies?
  • Abhi
    Abhi about 6 years
    Thanks . Adding - useLibrary 'org.apache.http.legacy' did the trick.
  • eltiare
    eltiare almost 6 years
    @Dan it's because the org.apache.http library was removed with version 23.
  • Scott Kohlert
    Scott Kohlert almost 5 years
    You're a lifesaver.
  • hamx0r
    hamx0r over 4 years
    To clarify, this doesn't go into the root build.gradle, but rather within the module folder you're building (ie "app")
  • doctorram
    doctorram over 3 years
    This is not a real solution! Look below for @Jinu's answer.