56

I have added this line to my build.gradle

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'

and I want to use MultipartEntityBuilder in my code. However Android studio doesn't add the library to my code. Can anyone help me with this?

Hirad Roshandel
  • 2,175
  • 5
  • 40
  • 63
  • Is that line in the top-level `dependencies` closure? Do you have `mavenCentral()` configured in the top-level `repositories` closure? – CommonsWare Sep 24 '14 at 19:29
  • @CommonsWare I'm new to this can you explain more please? what do you mean by top-level closure? and how can i check whether mavencentral is configured there – Hirad Roshandel Sep 24 '14 at 19:33
  • Given the errors you are reporting in your comments on the one answer below, you already have these parts set up. – CommonsWare Sep 24 '14 at 19:37
  • @CommonsWare why not `jcenter()`? It has more artifacts, better search, copy-paste snippets for Gradle and that's what Android Studio recommends. – JBaruch Sep 24 '14 at 21:52
  • @CommonsWare Everything you find on Maven Central is in JCenter as well, so you are safe with that. But both Gradle and Android Studio are much more `jcenter()` than Maven Central. There is also an outstanding issue to switch Gradle, Please to look in jcenter. – JBaruch Sep 24 '14 at 22:29
  • Refer to this link http://stackoverflow.com/questions/29782727/gradle-build-failed-apache-httpentity/32344127#32344127 – Rajesh Tiwari Sep 02 '15 at 03:26

8 Answers8

134

If you are using target SDK as 23 add the below code in your build.gradle

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

Additional note here: don't try using the gradle versions of those files. They are broken (28.08.15). I tried over 5 hours to get it to work. It just doesn't. not working:

compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'

Another thing don't use:

'org.apache.httpcomponents:httpclient-android:4.3.5.1'

It's referring to 21 API level.

Null
  • 1,950
  • 9
  • 30
  • 33
Jinu
  • 8,665
  • 4
  • 32
  • 37
  • It's not just target SDK, also happens if you use compile SDK 23, and proguard will still throw errors on that as well – Zac Sweers Aug 21 '15 at 21:24
  • After adding these three jars and already my project has google play service, fbook sdk, appcompat_v7 so I got and error of 65K method, can you please suggest proguard for above libs? – Chitrang Aug 24 '15 at 11:11
  • @Chitrang you can also make your app multidex ;):) – cV2 Aug 28 '15 at 12:23
  • @Chitrang use android{ useLibrary 'org.apache.http.legacy' } add this to your build.gradle file remove above compilefile dependencies – Jinu Aug 31 '15 at 11:21
  • @JINUJAYAKUMAR i'm getting useLibrary cannot resolved to a type and error Gradle DSL method not found 'useLibrary()' – chiru Sep 03 '15 at 17:08
  • did your target sdk is 23 and build tools is 23.0.0 use this link for more info http://developer.android.com/preview/behavior-changes.html#behavior-apache-http-client – Jinu Sep 04 '15 at 04:47
  • @chiru update your gradle. http://stackoverflow.com/questions/30856785/how-to-add-apache-http-api-legacy-as-compile-time-dependency-to-build-grade – android_dev Sep 07 '15 at 07:49
  • do not use multidex if possible, it multiplied compile time from 15seconds to 2minutes – James Tan Nov 15 '15 at 07:32
  • 2
    Simply adding "android{ useLibrary 'org.apache.http.legacy' }" resolved my issue. Thanks! – jwehrle Mar 05 '17 at 19:54
24

The accepted answer does not seem quite right to me. There is no point dragging a different version of HttpMime when one can depend on the same version of it.

compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5'
compile (group: 'org.apache.httpcomponents' , name: 'httpmime' , version: '4.3.5') {
    exclude module: 'org.apache.httpcomponents:httpclient'
}
ok2c
  • 26,450
  • 5
  • 63
  • 71
8

Try adding this to your dependencies:

compile 'org.apache.httpcomponents:httpclient:4.4-alpha1'

And generally if you want to use a library and you are searching for the Gradle dependency line you can use Gradle Please

EDIT: Check this one too.

Community
  • 1
  • 1
Pavlos
  • 2,183
  • 2
  • 20
  • 27
  • I get this error: Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0 WARNING: Dependency org.apache.httpcomponents:httpclient:4.4-alpha1 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages – Hirad Roshandel Sep 24 '14 at 19:31
  • Then there is no reason adding it if it's already included in the Android System! – Pavlos Sep 24 '14 at 19:33
  • but i cannot import the library – Hirad Roshandel Sep 24 '14 at 19:34
  • 1
    it says cannot resolve symbol 'MultipartEntityBuilder' and it doesn't recommend any import. – Hirad Roshandel Sep 24 '14 at 19:38
  • @HiradRoshandel: Hopefully there are more instructions somewhere, from the maintainers of the `httpclient-android` artifact, explaining how to properly use that artifact in an Android Studio/Gradle for Android project. – CommonsWare Sep 24 '14 at 19:39
  • @Pavlos is there any way not to get the alpha version? and get previous versions – Hirad Roshandel Sep 24 '14 at 21:36
  • You will have to search for previous jar versions i guess – Pavlos Sep 24 '14 at 22:34
8

None of the others worked for me. I had to add the following dependency, as explained here

compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'

because I was targeting API 23.

4

I resolved problem by adding following to my build.gradle file

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'
Nicks
  • 3,188
  • 3
  • 25
  • 29
2

I searched over and over this solution works like a charm ::

    apply plugin: 'com.android.application'
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.3"
        defaultConfig {
            applicationId "com.anzma.memories"
            useLibrary 'org.apache.http.legacy'
            minSdkVersion 15
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
 packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }
    buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile('org.apache.httpcomponents:httpmime:4.3.6') {
            exclude module: 'httpclient'
        }
        compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
        compile 'com.android.support:appcompat-v7:25.3.1'
        testCompile 'junit:junit:4.12'
    }
  • For future reference please separate the textual part of the answers from the code, to improve readability and quality of the answer. – Mirza Sisic Sep 25 '17 at 16:19
1

I don't know why but (for now) httpclient can be compiled only as a jar into the libs directory in your project. HttpCore works fine when it is included from mvn like that:

dependencies {
      compile 'org.apache.httpcomponents:httpcore:4.4.3'
}
Nikolay Hristov
  • 1,602
  • 1
  • 15
  • 22
0

Working gradle dependency

Try this:

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

CroMagnon
  • 1,218
  • 7
  • 20
  • 32
Ketan Ramani
  • 4,874
  • 37
  • 42