Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 594 forks source link

[BUG] NoSuchFieldError / files found with path 'META-INF/DEPENDENCIES' error on android #434

Closed ErrorxCode closed 2 years ago

ErrorxCode commented 2 years ago

Describe the bug Code fails for android. While compiling, we get files found with path 'META-INF/DEPENDENCIES' error. If we resolve this by excluding dependencies by adding packaging Block, then gets a runtime error NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier

To Reproduce Steps to reproduce the behavior: Just call the as[Type]() method and you will encounter this problem.

Expected behavior A request made to the URL

Stack trace

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)

Environmental Data:

Additional context The main problem is that while building APK, we can't include the META-INF/DEPENDENCIES'. And excluding it result in this error.

ryber commented 2 years ago

I'm not really clear on what the issue is but it's a problem with Apache HttpClient and not with Unirest

ErrorxCode commented 2 years ago

Unirest is using deprecated Components of httpclient. It is using AllowAllHostnameVerifier class which has been deprecated in version 4.4. You must use NoopHostnameVerifier instead.

Reference : https://hc.apache.org/httpcomponents-client-4.5.x/current/httpclient/apidocs/org/apache/http/conn/ssl/AllowAllHostnameVerifier.html

ryber commented 2 years ago

deprecated and gone are two different things though, the class hasn't been removed. Why are you excluding deprecated classes from your package?

ErrorxCode commented 2 years ago

Depreciation itself means that it should not be used anymore. It is causing problems in android thats why it is replaced with newer one. In android, you have to exclude Meta-INF files of library, otherwise it won't compile.

On Tue 15 Mar, 2022, 3:07 AM Ryan Bergman, @.***> wrote:

deprecated and gone are two different things though, the class hasn't been removed. Why are you excluding deprecated classes from your package?

— Reply to this email directly, view it on GitHub https://github.com/Kong/unirest-java/issues/434#issuecomment-1067320830, or unsubscribe https://github.com/notifications/unsubscribe-auth/APWEVDS6UO7NGDYMXZJX5WTU76WRJANCNFSM5QVWRNAA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

ryber commented 2 years ago

what do you mean by

thats why it is replaced with newer one.

Replaced what? Apache? Did you upgrade it to 5.x? Because that won't work at all.

ryber commented 2 years ago

BTW I just checked and Unirest itself is NOT using AllowAllHostnameVerifier directly, Apache is, so again, this is more of an Apache issue https://github.com/Kong/unirest-java/search?q=AllowAllHostnameVerifier

ryber commented 2 years ago

@ErrorxCode what you are running into is that the SSLConnectionSocketFactory in Apache has a static field for a instance of AllowAllHostnameVerifier. There is no way to get around using SSLConnectionSocketFactory short of not doing TLS/SSL at all.

https://github.com/mydevotion/httpclient/blob/master/httpclient/src/main/java/org/apache/http/conn/ssl/SSLConnectionSocketFactory.java#L143-L145

I know many folk using both Unirest and Apache HttpClient 4 in Android applications so I'm sure this is not a systemic problem with Android and deprecated classes.

In any case this is all internal to Apache, closing this issue as there is nothing Unirest can do. You might ask the Apache Http Client developers.

ryber commented 2 years ago

Oh you could also try using the RC of Unirest-4 because it doesn't use Apache in favor of using the Java native client

ErrorxCode commented 2 years ago

Could you please share me the link ?

On 15-Mar-2022 11:38 PM, "Ryan Bergman" @.***> wrote:

Oh you could also try using the RC of Unirest-4 because it doesn't use Apache in favor of using the Java native client

— Reply to this email directly, view it on GitHub https://github.com/Kong/unirest-java/issues/434#issuecomment-1068299689, or unsubscribe https://github.com/notifications/unsubscribe-auth/APWEVDWDA3P74L7HPOUHPXTVADG3JANCNFSM5QVWRNAA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

ryber commented 2 years ago

https://github.com/Kong/unirest-java/discussions/429