19

I'm using retrofit (2.7.2) and OkHttp version (4.4.0) in an Android project and I'm facing a crash on a request.

Dependencies:

kapt("com.squareup.moshi:moshi-kotlin-codegen:1.9.2")
implementation 'com.squareup.moshi:moshi:1.9.2'     
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:converter-moshi:2.7.2'

implementation("com.squareup.okhttp3:okhttp:4.4.0")
implementation("com.squareup.okhttp3:okhttp-tls:4.4.0")
implementation "com.squareup.okhttp3:logging-interceptor:4.4.0"

Crash:

java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classes (declaration of 'okhttp3.Cookie' appears in /data/app/com.package-1/base.apk:classes3.dex) at okhttp3.JavaNetCookieJar.saveFromResponse(JavaNetCookieJar.java:45) at com.facebook.react.modules.network.ReactCookieJarContainer.saveFromResponse(ReactCookieJarContainer.java:36) at okhttp3.internal.http.HttpHeaders.receiveHeaders(HttpHeaders.kt:207) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:85) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:502) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

2
  • latest version of okhttp3 is 4.4.1. change from 4.4.0 to 4.4.1. refer this search.maven.org/search?q=g:com.squareup.okhttp3 Commented Mar 9, 2020 at 8:55
  • @JosePraveen updated okhttp version to 4.4.1 still getting same crash Commented Mar 9, 2020 at 10:18

4 Answers 4

37

You're pulling in an old version of okhttp-urlconnection, and it is incompatible with the current version of the OkHttp core library.

You can fix by adding an explicit dependency on okhttp-urlconnection:

implementation("com.squareup.okhttp3:okhttp-urlconnection:4.4.1")

Or by adopting OkHttp’s new BOM for versions:

dependencies {
   implementation(platform("com.squareup.okhttp3:okhttp-bom:4.4.1"))
   implementation("com.squareup.okhttp3:okhttp")              // No version!
   implementation("com.squareup.okhttp3:okhttp-urlconnection") // No version!
}
Sign up to request clarification or add additional context in comments.

2 Comments

Adding implementation("com.squareup.okhttp3:okhttp-urlconnection:4.4.1") after flipper stuff in app/build.gradle worked for me :) Not sure position is relevant though
I tried first solution and it worked for me.
13

I used following okhttp3 versions and woked fine for me...

implementation 'com.squareup.okhttp3:okhttp:4.7.2'
implementation 'com.squareup.okhttp3:logging-interceptor:4.4.1'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:4.4.1'

1 Comment

I'm getting The server may not support the client's requested TLS protocol versions: (TLSv1.2). You may need to configure the client to allow other protocols to be used. Is the package outdated?
1

Please make sure that build.gradle file hase a same version for belowed dependencies and if you have this below version and getting above error than change version for them and your issue has been solved.:

implementation 'com.squareup.okhttp3:logging-interceptor:4.8.0' implementation 'com.squareup.okhttp3:okhttp:4.8.0'

Change the version with below :

implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1' implementation 'com.squareup.okhttp3:okhttp:3.4.1'

Comments

0

I wanted to add that this error was showing up after ejecting from Expo in React Native.

The solution was to go to add the following to android/app/build.gradle (inside dependencies):

    compile "com.squareup.okhttp3:okhttp:4.2.1"
    compile "com.squareup.okhttp3:logging-interceptor:4.2.1"
    compile "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"

They would look like this:

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules
   
    // you can insert it here
    compile "com.squareup.okhttp3:okhttp:4.2.1"
    compile "com.squareup.okhttp3:logging-interceptor:4.2.1"
    compile "com.squareup.okhttp3:okhttp-urlconnection:4.2.1"
   
}

(Note: Check the versions in the Maven Repository

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.