Search code examples
androidapacheandroid-studioandroid-sdk-tools

Conflict between Android SDK and Apache HTTP after API 23 update


In my project I am using these libraries:

compile 'org.apache.httpcomponents:httpclient:4.5.2'
compile 'org.apache.httpcomponents:httpcore:4.4.4'

Everything worked fine, until I updated Android Tools for API 23. Then when I try to perform HttpPost I get the following exception:

java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:966)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)

And this is the library code where it happens:

public class SSLConnectionSocketFactory implements LayeredConnectionSocketFactory {

public static final String TLS   = "TLS";
public static final String SSL   = "SSL";
public static final String SSLV2 = "SSLv2";

@Deprecated
public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
    = AllowAllHostnameVerifier.INSTANCE;

When looking at the external libraries, I can see the AllowAllHostnameVerifier class under android.jar:

AllowAllHostnameVerifier under android.jar

And when using the code from the answer on this question: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

I get this output:

jar:file:/E:/Program%20Files/Eclipse%20Android%20SDK/adt-bundle-windows-x86_64-20140702/sdk/platforms/android-23/android.jar!/org/apache/http/conn/ssl/AllowAllHostnameVerifier.class

So I can see the build is taking this class from the SDK and not from the Apache libraries.

So the question is, how can I fix my classpath or other configuration necessary to direct the build to use these classes from Apache as before and not from the SDK?

Thanks in advance!


Solution

  • In short, just edit the android.jar file, remove the classes need being replaced by Apache libraries.