ChatGPT解决这个技术问题 Extra ChatGPT

Android Gradle Apache HttpClient does not exist?

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.


J
Jarett Millard

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


change your buildscript to classpath 'com.android.tools.build:gradle:1.3.0'
Already had 1.3.1. Still cannot resolve classes inside Android Studio: Error:(14, 23) error: package org.apache.http does not exist
android studio 1.4 beta 2
And what if you want to use the latest version of apache http components?
Thanx Jinu, updating Android Studio from 1.3 to above version is must. Got it working
U
Uwais A

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.


For 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
@ssuukk Hi, did you find a solution for this error?
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.
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.
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' }}
C
CopsOnRoad

Add this library into build.gradle

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

can someone explain this? Why does this make it work? Why do we need to add this line, aside from adding the dependencies?
@Dan it's because the org.apache.http library was removed with version 23.
To clarify, this doesn't go into the root build.gradle, but rather within the module folder you're building (ie "app")
B
Benjamin Piette

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();

Hey @Benjamin, how to send URL with PUT method?
@TrickySolutions Sorry no idea, haven't used the PUT method yet.
This is not a real solution! Look below for @Jinu's answer.
H
Hamid Zandi

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'])

In another of my projects this proved to be the only answer that worked so +1 and thanks.
why isnt this the answer?
Same for me. Someone should accept this answer instead the one accepted now.
This is the least path of resistance, not sure I want to down-grade gradle. It compile fine for me.
Thanks! This works with the gradle-experimental 0.6.0-alpha5 plugin. "useLibrary" doesn't seem to exist in the experimental plugin.
C
CopsOnRoad

Basically all you need to do is add:

useLibrary  'org.apache.http.legacy'

To your build.gradle file.


D
Daniel Nugent

I ran into similar problems, you might be able to get it to work using a similar method.

First, try this with your current configuration, exclude httpclient from httpmime:

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile ('org.apache.httpcomponents:httpmime:4.2.6'){
            exclude module: 'httpclient'
        }
    compile 'org.apache.httpcomponents:httpclient:4.2.6'
} 

In my case, I fixed it by using the following jars :

httpclient-android-4.3.5.1.jar

httpmime-4.3.5.jar

Then, in the build.gradle, excluding httpclient from httpmime:

dependencies {
    compile 'com.google.android.gms:play-services:+'
    compile('org.apache.httpcomponents:httpmime:4.3.5') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
}

Thanks for your answer, im afraid neither worked for me though :(
@tur that's strange, the second option worked for me. Make sure that your old jar files are deleted for the second option.
What is libs/core.jar for?
M
Martin Pfeffer

I ran into the same issue. Daniel Nugent's answer helped a bit (after following his advice HttpResponse was found - but the HttpClient was still missing).

So here is what fixed it for me:

(if not already done, commend previous import-statements out) visit http://hc.apache.org/downloads.cgi get the 4.5.1.zip from the binary section unzip it and paste httpcore-4.4.3 & httpclient-4.5.1.jar in project/libs folder right-click the jar and choose Add as library.

Hope it helps.


S
Sophia

Perfect Answer by Jinu and Daniel

Adding to this I solved the Issue by Using This, if your compileSdkVersion is 19(IN MY CASE)

compile ('org.apache.httpcomponents:httpmime:4.3'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'commons-io:commons-io:1.3.2'

else if your compileSdkVersion is 23 then use

android {
useLibrary 'org.apache.http.legacy'
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    }
}

N
NOT_A_PROGRAMMER

This is what I did, and it works for me.

step 1: add this in the build.grade(module: app)

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

step 2: sync the project and done.


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 ?
I
Immy

After spending days on the subject, I managed to solve it finally. Unfortunately we need it if we want to use expansion files. The solution has been provided by a few people here, but for me the answers were missing a few minor detail that could have saved a lot of time for me. Here is the sequence of events for novices like me to save your precious time.

First I imported the "library" folder from D:\Users\Imre\AppData\Local\Android\sdk1\extras\google\play_licensing to the project by going into File, New, Import Module and navigating to the "library" folder. If you open the SDK Manager and click on "Launch Standalone SDK Manager" in the bottom of the popup screen, you can hover your pointer over the "Extras" folder down in the bottom and the small yellow info will tell you where you will find the package that you need to import.

Once that is done, go down to the "library" section in the left pane with "Project" and within it the "Android" tab open. Open the java section of the library and open the APKExpansionPolicy class. If you have errors and the import org.apache.http.NameValuePair and the import org.apache.http.client.utils.URLEncodedUtils are pale grey, open the build.gradle(Project:whatever) and make sure that in "buildscript" under "dependancies" you have the "classpath 'com.android.tools.build:gradle:1.5.0" included. The 1.5.0 might be different in your case. I guess it depends on your Studio version. Mine is 1.5.1. If yours is newer, you will be reminded to update the numbers.

After that, go to "build.gradle(Module:library) and include the "useLibrary 'org.apache.http.legacy' into the "android"section. Sync Now (R/H top corner) should be offered. Do it.

If you get further error messages (I wouldn't know it because I did it the other way round), the Apache files might be missing from your project, as apache is not supported anymore. Find them in C/D:Users/Yourname/AppData/Local/Android/Sdk/platforms/android-23/optional and copy/paste them into yourproject/app/libs folder. You should have an optional.json and an org.apache.http.legacy.jar file in there. You might be better off going to apache's website and dowloading the newest version: http://hc.apache.org/downloads.cgi Unzip it anywhere you want, open it, go to "lib", copy the httpclient-4.5.1.jar file and replace the org.apache.http.legacy.jar file with it.

Sync and rebuild/clean your project and it should be good to go.

Just in case, open your terminal (L/H bottom of Android Studio) and type in "gradlew clean". It will install a few things. Once it is done, type in "gradlew assemble". It will take a few minutes to finish, but it will give you the errors if you have any of course. If you cannot type in anything in the Terminal, launch command prompt (Windows button+R), type in cmd, hit OK, right click on the title of the small black popup screen (C:\WINDOWS\system32\cmd.exe), Properties, Options, in the bottom of the window check that "Use legacy console" is thicked. Restart A.Studio.

Good luck!


C
Community

The version of the Apache HTTP client provided on stock Android was very very old.

Google Android 1.0 was released with a pre-BETA snapshot of Apache HttpClient. To coincide with the first Android release Apache HttpClient 4.0 APIs had to be frozen prematurely, while many of interfaces and internal structures were still not fully worked out. As Apache HttpClient 4.0 was maturing the project was expecting Google to incorporate the latest code improvements into their code tree. Unfortunately it did not happen.

While you could keep using the old deprecated library via the useLibrary 'org.apache.http.legacy' workaround (suggested by @Jinu and others), you really need to bite the bullet and update to something else, for example the native Android HttpUrlConnection, or if that doesn't meet your needs, you can use the OkHttp library, which is what HttpUrlConnection is internally based upon anyway.

OkHttp actually has a compatibility layer that uses the same API as the Apache client, though they don't implement all of the same features, so your mileage may vary.

While it is possible to import a newer version of the Apache client (as suggested by @MartinPfeffer), it's likely that most of the classes and methods you were using before have been deprecated, and there is a pretty big risk that updating will introduce bugs in your code (for example I found some connections that previously worked from behind a proxy no longer worked), so this isn't a great solution.


D
Debasish Mitra

I had to post as none of the above answers worked completely for me.

I am using Android Studio

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

compileSdkVersion 23
buildToolsVersion "23.0.3"

Step 1: Download lastest jar file (http://www-eu.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.2-bin.zip)

Step 2: Copy paste the .jar file to the libs folder (create if does not exist already) in your module (can be app or library)

Step 3: Right click on the jar and "Add as Library". It will automatically add the jar file as a dependency in your module's gradle file

Step 4: Now automatically your problem will get resolved but in case you are using proguard in your app, it will give you warning about duplicate class files and won't let you build. It is a known bug and you need to add following to your proguard-rules

-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**

Good Luck!


S
Simon

In my case, I updated one of my libraries in my android project.

I'm using Reservoir as my cache storage solution: https://github.com/anupcowkur/Reservoir

I went from:

compile 'com.anupcowkur:reservoir:2.1'

To:

compile 'com.anupcowkur:reservoir:3.1.0'

The library author must have removed the commons-io library from the repo so my app no longer worked.

I had to manually include the commons-io by adding this onto gradle:

compile 'commons-io:commons-io:2.5'

https://mvnrepository.com/artifact/commons-io/commons-io/2.5


L
Likwidsage

I cloned the following: https://github.com/google/play-licensing

Then I imported that into my project.