Android Gradle Apache HttpClient does not exist?
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'])
Related videos on Youtube
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, 2020Comments
-
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 almost 9 yearsThanks for your answer, im afraid neither worked for me though :(
-
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 over 8 yearsFor me it failed with: Error:Execution failed for task ':xenoAmp:mergeDebugJavaResources'. > Cannot determine expansion folder for L:\Projekty\xenoampgit\xenoAmp\build\intermediates\packagedJarsJavaResources\debug\httpclient-android-4.3.5.1.jar1037172435\META-INF\LICENSE with folders L:\Projekty\xenoampgit\xenoAmp\build\intermediates\sourceFolderJavaResources\debug,L:\Projekty\xenoampgit\xenoAmp\build\intermediates\packagedJarsJavaResources\debug
-
Henrique de Sousa over 8 yearsSupplied String module notation 'org.apache.http.legacy' is invalid.
-
Jinu over 8 yearschange your buildscript to classpath 'com.android.tools.build:gradle:1.3.0'
-
Henrique de Sousa over 8 yearsAlready had 1.3.1. Still cannot resolve classes inside Android Studio:
Error:(14, 23) error: package org.apache.http does not exist
-
Jinu over 8 yearsis your targetSdkVersion 23 and build tools 23.0.0
-
Henrique de Sousa over 8 yearsYes, of course. Do your imports work fine? What Android Studio version are you using?
-
Jinu over 8 yearsandroid studio 1.4 beta 2
-
CeccoCQ over 8 years@ssuukk Hi, did you find a solution for this error?
-
ssuukk over 8 yearsUnfortunately 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 over 8 yearsLife saver response. Thanks!
-
IgorGanapolsky over 8 yearsWhat is
libs/core.jar
for? -
russellhoff over 8 yearsAnd what if you want to use the latest version of apache http components?
-
Jinu over 8 years@Silviu you can find dependency from here gradleplease.appspot.com/#httpmime
-
John81 over 8 yearsAgree 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 over 8 yearsThanx Jinu, updating Android Studio from 1.3 to above version is must. Got it working
-
Apqu over 8 yearsIn another of my projects this proved to be the only answer that worked so +1 and thanks.
-
Alexiscanny over 8 yearsI 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 over 8 yearswhy isnt this the answer?
-
Ivan Fazaniuk over 8 yearsSame for me. Someone should accept this answer instead the one accepted now.
-
Ed Manners over 8 yearsThis is the least path of resistance, not sure I want to down-grade gradle. It compile fine for me.
-
monkey0506 over 8 yearsThanks! This works with the gradle-experimental 0.6.0-alpha5 plugin. "useLibrary" doesn't seem to exist in the experimental plugin.
-
RoFF about 8 yearsit failed to resolve with error "Failed to resolve: org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2". I am using experimental gradle 0.6.0-beta. can you figure it out ?
-
user961524 about 8 yearshelped without disturbing the other settings. thanks
-
Bhushan Shirsath about 8 yearsi used this tutorial blazin.in/2016/03/http-connection-for-android-marshmallow.html it worked for me using HttpOpenUrlConnection clases
-
Naveed Ahmad about 8 yearsWorking, Thanks. 'org.apache.http.legacy' helped me, gradle was updated previously
-
mfaisalhyder almost 8 yearshelped in June 2016 :D (y) Eclipse still FTW :p
-
natsuki_2002 almost 8 yearsYea buddy! Thanks! great answer
-
maimoona over 7 yearsGreat answer! Tried alot of other answers but changing one things broke other. This solution worked for me without change any existing configuration
-
Amrut Bidri over 7 yearsWorked for me .. Thanks a lot.
-
Dharma Sai Seerapu over 7 yearsHey @Benjamin, how to send URL with PUT method?
-
Benjamin Piette over 7 years@TrickySolutions Sorry no idea, haven't used the PUT method yet.
-
Dan almost 7 yearscan someone explain this? Why does this make it work? Why do we need to add this line, aside from adding the dependencies?
-
Abhi about 6 yearsThanks . Adding - useLibrary 'org.apache.http.legacy' did the trick.
-
eltiare almost 6 years@Dan it's because the org.apache.http library was removed with version 23.
-
Scott Kohlert almost 5 yearsYou're a lifesaver.
-
hamx0r over 4 yearsTo clarify, this doesn't go into the root
build.gradle
, but rather within the module folder you're building (ie "app") -
doctorram over 3 yearsThis is not a real solution! Look below for @Jinu's answer.