2

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!

Community
  • 1
  • 1
Nom1fan
  • 846
  • 2
  • 11
  • 27
  • 1
    I hope this help you some http://stackoverflow.com/questions/27441398/how-to-put-my-libraries-in-front-of-android-jar-by-editing-build-gradle-in-andro – Jiang YD Apr 28 '16 at 08:36

1 Answers1

0

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

Jiang YD
  • 3,205
  • 1
  • 14
  • 20
  • Hi! Thanks for answering. I tried to right click->Delete and confirm but it seems it does not do anything. Is there a more proper way to edit android.jar? – Nom1fan Apr 28 '16 at 08:55
  • 1
    unzip it to a folder, delete files and zip again. it should work. – Jiang YD Apr 28 '16 at 08:58
  • I tried what you said, and it seemed good but when building I am getting this exception: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '\build-tools\23.0.3\aapt.exe'' finished with non-zero exit value 1 – Nom1fan Apr 28 '16 at 09:10
  • @Nom1fan wait a minute, I'll try it in my IDE – Jiang YD Apr 28 '16 at 09:21
  • I just tried it, it works here. please attach more error information. – Jiang YD Apr 28 '16 at 09:27
  • Ok I built using flag --info and got this error in the output: ERROR: Asset package include 'E:\Program Files\Eclipse Android SDK\adt-bundle-windows-x86_64-20140702\sdk\platforms\android-23\android.jar' not found. It seems it cannot find the new android.jar I created. Although I can see it's there on Windows Explorer. Did I do something wrong? I extracted the .jar with WinRAR, deleted the unwanted libraries and then zipped back with Windows ZIP and renamed to .jar – Nom1fan Apr 28 '16 at 09:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110493/discussion-between-jiang-yd-and-nom1fan). – Jiang YD Apr 28 '16 at 09:58