'java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe
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)
Solution 1:[1]
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!
}
Solution 2:[2]
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'
Solution 3:[3]
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'
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jesse Wilson |
| Solution 2 | ultra.deep |
| Solution 3 | MEGHA DOBARIYA |
| Solution 4 | Jose A |
