Java – How do I override Android API classes using classes available in the added jar?

How do I override Android API classes using classes available in the added jar?… here is a solution to the problem.

How do I override Android API classes using classes available in the added jar?

Scenario: An android project says, ProjAndroid references a java project (HelperProj).

In HelperProj, I tried using httpConnection Pooling by instantiating its manager.

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

I came across the Apache example and was able to implement it as a standalone program without any glitches.

But for android, first it gave the NoClassDefFoundError error, when I added httpclient-4.3.1.jar and httpcore-4.3.jar even to the android project. But now I’m getting another error

Caused: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE

My conclusion: –
The above field should be Available in httpcore-4.3.jar because I didn’t get any error when I ran the code in standalone mode. So, I searched and found that Android already provides BasicLineFormatter class if this field is not available and may be referenced at run time.

please correct me in case the above is wrong.

Help :-
So please someone guide me on how to resolve this resource conflict and use classes available in jar instead of classes available in android api.

Solution

So someone please guide me as how may I resolve this resource conflict and use the class available in jar instead of the one available with android api.

This is not directly possible. You do not control the classpath, so classes loaded by the firmware always take precedence.

You are welcome to try < a href="http://jarjar.googlecode.com/" rel="noreferrer noopener nofollow"> utilities like jarjar to move third-party code into a separate package.

Alternatively, you can see if someone else has already done this for you, as with htpclientandroid

Alternatively, you are welcome to propose a different solution to your problem that does not involve replacing the classes provided by Android.

Related Problems and Solutions