square / square-java-sdk

Java client library for the Square API
https://developer.squareup.com
Other
60 stars 33 forks source link

Wrong okhttp3 version causes NoSuchMethodError #40

Closed danielkpl2 closed 1 year ago

danielkpl2 commented 3 years ago

Most of your newer square library versions (including 6.5.0.20201028, 7.0.0.20201118, 8.0.0.20201216, 8.1.0.20210121) reference an old version of okhttp3 3.8.1 which doesn't have the callTimeout method in OkHttpClient.Builder which causes the error:

java.lang.NoSuchMethodError: okhttp3.OkHttpClient$Builder.callTimeout(JLjava/util/concurrent/TimeUnit;)Lokhttp3/OkHttpClient$Builder;

Temporary solution is to exclude the okhttp3 library from com.squareup.square and include a newer version of okhttp3:

<dependencies>
    ...
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.14.9</version>
    </dependency>
    <dependency>
        <groupId>com.squareup</groupId>
        <artifactId>square</artifactId>
        <version>8.1.0.20210121</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<dependencyManagement>
         ...
        <dependencies>
            <dependency>
                <groupId>com.squareup</groupId>
                <artifactId>square</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>com.squareup.okhttp3</groupId>
                        <artifactId>okhttp</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

A permanent solution would be to include a newer version of okhttp3 in com.squareup.square.

StephenJosey commented 3 years ago

Thanks for sharing, I'll relay this to our SDK team and update this with any new info.

tsznober commented 2 years ago

I'm getting this error when trying to call the createCustomer and createCustomerAsync API calls from SDK version 18.3.0.20220316 In my case it seems to be mapping to buildDeleteCustomerRequest.

UPDATE: I tried knocking back the SDK release version in my POM and found that 17.0.0.20211117 is when the createCustomerAsync works. All of the releases since then fail with the NoSuchMethod error.

tsznober commented 1 year ago

I managed to fix the issue by explicitly adding the okhttp dependency. It seems that other dependencies in my project also used okhttp, but a different version.

Here is a snippet from my pom.xml file

    <dependency>
      <groupId>com.squareup</groupId>
      <artifactId>square</artifactId>
      <version>25.0.1.20221019</version>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.10.0</version>
    </dependency>

I hope this helps anyone else who encounters this problem.

Maybe the SDK team and add something in the documentation to point this out.

zenmasterjobo commented 1 year ago

The Square SDK Pom file no longer has okHTTP as a dependency. If members of the community run into this error or similar again, feel free to reopen.