Java – How to use Apache http-client 4.5.x with external libraries on Android

How to use Apache http-client 4.5.x with external libraries on Android… here is a solution to the problem.

How to use Apache http-client 4.5.x with external libraries on Android

I’m trying to understand Android’s current relationship with the Apache http client. I need to use a standard Java library that relies on , which seems impossible on org.apache.httpcomponents:httpclient:4.5.2 Android.

We can see that in Android M, support was removed for http clients. In Android P, libraries were removed from the boot class path and are not available for applications that do not have a list entry. I can also see that there is an official Apache Android port of httpclient which is a suitable drop-in replacement for 4.3.5.1 if you need a slightly more modern version of the library. There’s even a third party port of 4.4.1.1 for apps with modern target SDKs.

The minimum SDK for my application is 17 and the target SDK is 28. So my first question is, is it really possible to terminate any reference to the android version of apache httpclient with the minimum sdk stuck at 17, and if not, how can I replace that version with 4.5.2 in the library?

My specific error is that Android is java.lang.NoSuchFieldError: org.apache.http.conn.ssl.AllowAllHostnameVerifier.INSTANCEstill looking for and using legacy AllowAllHostnameVerifier.java classes without INSTANCE fields even though my target SDK is 28:

enter image description here

Best Solution

You cannot directly replace classes contained in the Android framework. The only solution is to use a different namespace. That’s why Spongy Castle uses org.spongycaSTLe.* instead of , and the items you link use cz.msebera.android.httpclient.*instead org.bouncycaSTLe.*of org.apache.http.*.

Unfortunately, this means that you have to change all references to http-client in the library.

Since the release of Android Jetpack, Jetifier, a translation tool, has been included in the SDK
A reference in the library bytecode at build time. There is a standalone version that can be configured using custom mappings.

In your case, this means:

  • Create a custom configuration to convert to org.apache.httpcz.msebera.android.httpclient
  • Convert your standard Java libraries
  • Use revamped libraries and third-party ports for http-client

Another possible solution for converting libraries is to use Jar Jar .

Related Problems and Solutions