[android] 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.

The answer is


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!


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.


Basically all you need to do is add:

useLibrary  'org.apache.http.legacy'

To your build.gradle file.


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

Then I imported that into my project.


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

Add this library into build.gradle

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

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


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!


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.


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:

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

Hope it helps.


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

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 :

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

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.


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


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-studio

A failure occurred while executing com.android.build.gradle.internal.tasks "Failed to install the following Android SDK packages as some licences have not been accepted" error Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util This version of Android Studio cannot open this project, please retry with Android Studio 3.4 or newer WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Flutter plugin not installed error;. When running flutter doctor ADB.exe is obsolete and has serious performance problems Android design support library for API 28 (P) not working Flutter command not found How to find the path of Flutter SDK

Examples related to intellij-idea

IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Error: Java: invalid target release: 11 - IntelliJ IDEA IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 ERROR Source option 1.5 is no longer supported. Use 1.6 or later Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How to configure "Shorten command line" method for whole project in IntelliJ intellij idea - Error: java: invalid source release 1.9 Failed to resolve: com.google.android.gms:play-services in IntelliJ Idea with gradle

Examples related to gradle

Gradle - Move a folder from ABC to XYZ A failure occurred while executing com.android.build.gradle.internal.tasks Gradle: Could not determine java version from '11.0.2' Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Failed to resolve: com.android.support:appcompat-v7:28.0 Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion Error - Android resource linking failed (AAPT2 27.0.3 Daemon #0)

Examples related to android-gradle-plugin

WARNING: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()' Android Material and appcompat Manifest merger failed Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries Invoke-customs are only supported starting with android 0 --min-api 26 error: resource android:attr/fontVariationSettings not found Exception : AAPT2 error: check logs for details Could not resolve com.android.support:appcompat-v7:26.1.0 in Android Studio new project Failed to resolve: com.android.support:appcompat-v7:27.+ (Dependency Error)