twilio / twilio-java

A Java library for communicating with the Twilio REST API and generating TwiML.
MIT License
482 stars 421 forks source link

java.lang.NoSuchFieldError #744

Closed GreenLava786 closed 1 year ago

GreenLava786 commented 1 year ago

Issue Summary

Code Snippet

# paste code here
``` Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
            val message = Message.creator(
                PhoneNumber("+916395604198"),
                PhoneNumber("+12544707932"),
                "Hi,Bro").create();

            System.out.println(message.sid);

### Exception/Log

paste exception/log here


    Process: com.wizion.contactsapp, PID: 3395
    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!classes4.dex)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.getSocketFactory(SSLConnectionSocketFactory.java:194)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:116)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:123)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:70)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:45)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:36)
        at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:143)
        at com.twilio.Twilio.buildRestClient(Twilio.java:202)
        at com.twilio.Twilio.getRestClient(Twilio.java:174)
        at com.twilio.base.Creator.create(Creator.java:40)
        at com.wizion.contactsapp.ui.main.contacts.MessageComposeActivity.sendSMS(MessageComposeActivity.kt:64)
        at com.wizion.contactsapp.ui.main.contacts.MessageComposeActivity.init$lambda$0(MessageComposeActivity.kt:48)
        at com.wizion.contactsapp.ui.main.contacts.MessageComposeActivity.$r8$lambda$W-TCP6r7k4_B2pHtzQZJCSIytHU(Unknown Source:0)
        at com.wizion.contactsapp.ui.main.contacts.MessageComposeActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7506)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1202)
        at android.view.View.performClickInternal(View.java:7483)
        at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
        at android.view.View$PerformClick.run(View.java:29334)
        at android.os.Handler.handleCallback(Handler.java:942)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7872)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

### Technical details:
* twilio-java version: 9.6.0
* java version:11.0.15
DanielErnestoThompson commented 1 year ago

The error message indicates that there's an issue with the version of org.apache.httpcomponents:httpclient that you are using. The Twilio SDK depends on this library, and it appears that the version of this library you are using doesn't have the AllowAllHostnameVerifier.INSTANCE field, which the SDK is trying to use.

The simplest way to resolve this issue would be to ensure that you're using a version of org.apache.httpcomponents:httpclient that's compatible with the Twilio SDK. According to the Twilio SDK's build.gradle file, it depends on version 4.5.13 of this library.

If you're using Gradle, you can specify this version in your build.gradle file like so:

groovy

**dependencies { // Other dependencies...

implementation 'org.apache.httpcomponents:httpclient:4.5.13'

}**

If you're using Maven, you can specify this version in your pom.xml file like so:

xml

**

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

**

You should also make sure that there are no other libraries in your classpath that are including different versions of the httpclient library, as this could also cause conflicts.

If the problem persists, you might want to consider downgrading your Twilio SDK to a version that's compatible with your current httpclient library. However, you'd need to check the Twilio SDK's documentation to find out which version this would be.

Hope this helps!

shweta-7span commented 1 year ago

Hey @DanielErnestoThompson, Thank you for answer this question. I checked in my project it is using '4.5.13' version for httpClient. I'm still getting this error. Below I put the full error. Please, let me know if anything I'm missing.

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:116)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:123)
    at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:70)
    at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:45)
    at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:36)
    at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:143)
    at com.twilio.Twilio.buildRestClient(Twilio.java:202)
    at com.twilio.Twilio.getRestClient(Twilio.java:174)
    at com.twilio.base.Creator.create(Creator.java:40)
    at com.example.demomaskedcall.Example.main(Example.java:29)

I did the below code:

Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Call call = Call.creator(
                        new com.twilio.type.PhoneNumber("MY_PHONE_NUMBER"),
                        new com.twilio.type.PhoneNumber("TWILIO_PHONE_NUMBER"),
                        URI.create("http://demo.twilio.com/docs/voice.xml"))
                .create();

        System.out.println("Masked Call: " + call.getSid());
GreenLava786 commented 1 year ago

I have made a custom API call for Twilio Api's using Retrofit to send SMS and receive an SMS list." You can check this at: https://github.com/waseem-i7/Contact-App. If you don't understand anything, please let me know.

AsabuHere commented 1 year ago

Hi @GreenLava786 and shweta-7span, I tested the code pasted in the thread, with twilio-java version 9.6.0 and java version 11.0.19 and this is working for me. Here is the configuration details for my project

org.apache.httpcomponents
  <artifactId>httpclient</artifactId>
  <version>4.5.13</version>

and

org.apache.httpcomponents
  <artifactId>httpcore</artifactId>
  <version>4.4.13</version>
AsabuHere commented 1 year ago

Hi @GreenLava786 and @shweta-7span Are you trying to run this inside your android application? if yes you might find the below information helpful The twilio-java library is not intended for use in Android applications. If you were to use it within an Android application you would expose your Application SID and Auth Token. If a malicious user were to discover this, they could abuse your account.

Instead, we recommend that you set up a server from which you send the messages and make requests to that server from your Android application. Check out this post on sending SMS messages from your Android app for more details.

shweta-7span commented 1 year ago

Yes, I made the "Example.Java" class inside the app folder of android app project and run it with "Coverage". I will do from the above blog which you shared.

Thank you @AsabuHere :smiley:

sharunthomas commented 1 year ago

I got the same error with the twilio-java version 9.7.0, with 'httpclient', version: '4.5.2' and 'httpcore', version: '4.4.4' The error log is

java.lang.NoSuchMethodError: org.apache.http.impl.client.DefaultRedirectStrategy.<init>([Ljava/lang/String;)V
        at com.twilio.http.HttpClient.<init>(HttpClient.java:35)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:54)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:45)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:36)
        at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:143)

the issue got fixed when I changed the Http client version to version: '4.5.12' , (Only the client version) and I'm using java version 8, should I update the HTTP core version too?

AsabuHere commented 1 year ago

Hi @GreenLava786, Closing this issue since twilio-java library is not intended for use in Android applications.

@sharunthomas Please open a new issue with the details of the environment and use case to track this