Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 616
A B Avatar asked Mar 09 '20 08:03

A B


1 Answers

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!
}
like image 195
Jesse Wilson Avatar answered Sep 17 '22 14:09

Jesse Wilson