Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Apache http-client 4.5.x used by external library on Android

I am trying to understand Android's current relationship to Apache http-client. I need to use a standard Java library which relies on org.apache.httpcomponents:httpclient:4.5.2 and it appears to be impossible on Android.

We can see that in Android M, support was removed for httpclient. And in Android P, the library was removed from the boot class path and is unavailable to apps without a manifest entry. I can also see that there is an official Apache Android port of httpclient which is a suitable direct replacement for 4.3.5.1 if you need a slightly more modern version of the library. And that there is even a third party port of 4.4.1.1 for apps with a modern target sdk.

My app's min sdk is 17, and target sdk is 28. So my first question is, would it actually be possible to kill any reference to the Android version of apache httpclient with a min sdk stuck at 17, and if not how can I replace that version with 4.5.2 which is inside the library.

My specific error is java.lang.NoSuchFieldError: org.apache.http.conn.ssl.AllowAllHostnameVerifier.INSTANCE, and even though I have a target SDK of 28, Android is still finding and using a legacy AllowAllHostnameVerifier.java class with no INSTANCE field:

enter image description here

like image 220
Daniel Wilson Avatar asked Feb 12 '19 14:02

Daniel Wilson


People also ask

What is HttpClient Android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.

What is Apache HttpClient used for?

Apache HttpClient is a popular Java library providing efficient and feature-rich packages implementing the client-side of the most recent HTTP standards. The library is designed for extension while providing robust support for the base HTTP methods. In this tutorial, we'll look at the Apache HttpClient API design.

How do I import an HttpClient in Java?

Java HttpClient GET requestHttpClient client = HttpClient. newHttpClient(); A new HttpClient is created with the newHttpClient factory method. HttpRequest request = HttpRequest.

What is the difference between HttpClient and CloseableHttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .


1 Answers

You can not directly replace classes included in the Android framework. The only solution is to use a different namespace. This is why Spongy Castle uses org.spongycastle.* instead of org.bouncycastle.*, and the project you linked uses cz.msebera.android.httpclient.* instead of org.apache.http.*.

Unfortunately, this means that you have to change every reference to http-client in your library.

Since the release of Android Jetpack, the SDK includes Jetifier, a tool to translate references in libraries bytecode at buildtime. There is a standalone version, and it can use custom mapping config.

In you case this would imply:

  • Create a custom config translating org.apache.http to cz.msebera.android.httpclient
  • Convert your standard Java library
  • Use the transformed library and the third party port of http-client

Another possible solution to convert the library would be to use Jar Jar.

like image 93
bwt Avatar answered Sep 29 '22 01:09

bwt