HttpClient java.lang.NoSuchFieldError: No static field INSTANCE problem on Android new API

Problems in use

Although httpClient is not used anymore, it is not ruled out that some guys like me, used it in old projects, and let it be maintained. Hey, this is a real pit, it’s a bag that was abandoned a long time ago. Why do you still use it? I really don’t understand.

If you have the following problems, congratulations on your entry into the hole.

java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; 
in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier;
 or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar:classes2.dex)

This means that both the HttpClient and the Framework layer have AllowAllHostnameVerifierthis file, which leads to conflicts.

The situation where the problem occurred

  1. It may be caused by the introduction of the jar package, which is as follows:
implementation group: 'org.apache.httpcomponents' , name: 'httpclient', version: '4.5.12'

solution

  • Delete the above quote, that is, do not use the sentence above, neither use 4.5.12 foreign packages, use Android native
  • Add this sentence
android {
    
    
   
    useLibrary 'org.apache.http.legacy'

}

That is, only add useLibrary 'org.apache.http.legacy'this in the build file, and don't use other references to HttpClient.

to sum up:

I think it's better to change the package file early. It seems that these are all discarded. There is no need to waste time dealing with this. It is a bloody lesson, although it was solved in the end.

Guess you like

Origin blog.csdn.net/honeylife/article/details/105136442