2

I have the following gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/DEPENDENCIES'
    }

    defaultConfig {
        applicationId "com.guesstheurf.guesstheurf"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile (
            [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.5.0'],
            [group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.5.0'],
            [group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.5.0']
    )

    compile ('org.apache.httpcomponents:fluent-hc:4.4.1')
}

When I build this, I get warnings stating

Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for release as it may be conflicting with the internal version provided by Android.

This is a dependency relied on by httpcomponents:fluent-hc.

I found this source on dependency management(chapter 51.4.7: Excluding transitive dependencies) that indicates I can just exclude the transitive dependency that causes the conflict.

However when I change it to

compile ('org.apache.httpcomponents:fluent-hc:4.4.1') { exclude module: 'httpclient' }

the Gradle warning is indeed gone, but the problem still persists:

Caused by: java.lang.NoClassDefFoundError: org.apache.http.client.fluent.InternalEntityEnclosingHttpRequest at org.apache.http.client.fluent.Request.Post(Request.java:95)

The issue stays when I use

exclude group: 'org.apache.httpcomponents', module: 'httpclient'

as indicated in this answer.

When I use

configurations {
    all*.exclude module: 'httpclient'
}

the warning is gone as well, though the runtime error persists.

This source which suggests to force my own version using force = true doesn't fix the issue either.

Explicitly adding the httpclient-related dependencies as suggested here doesn't work either (and doesn't remove the warnings):

compile 'org.apache.httpcomponents:httpclient:4.4.1'
compile 'org.apache.httpcomponents:fluent-hc:4.4.1'
compile "org.apache.httpcomponents:httpmime:4.4.1"
compile 'org.apache.httpcomponents:httpcore:4.4.1'

How can I get fluent-hc to work? And why doesn't it simply select the newest version of the httpclient package since that is the default resolution strategy according to 51.2.3 Resolve version conflicts?

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170

2 Answers2

3

And why doesn't it simply select the newest version of the httpclient package since that is the default resolution strategy according

Because Android ships with an extremely outdated (pre-BETA1) version of HttpClient 4.0, which makes it impossible to use any version of stock HttpClient. Gradle plugin is apparently smart enough to warn you about incompatibility and possible conflicts.

You have two options here:

  1. use official Android port of HttpClient, which is API compatible with the version shipped with Android by default

    dependencies {
        compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
        compile group: 'org.apache.httpcomponents' , name: 'fluent-hc' , version: '4.3.5'
    }
    
  2. Repackage HttpClient into a different name space using scripts developed by Dirk Boye. Your mileage with the latest releases of HttpClient may vary.

    org.apache.http -> thank.you.google.org.apache.http
    
ok2c
  • 26,450
  • 5
  • 63
  • 71
0

Seems like when building with Android Studio 1.4 for API 23, this line causes "Failed to resolve" error:

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

Use this instead:

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

Download the jar here: http://mvnrepository.com/artifact/cz.msebera.android/httpclient/4.4.1.1

whyoz
  • 5,168
  • 47
  • 53