Home » Android » java – OkHttpClient broken after updated Retrofit to Retrofit 2

java – OkHttpClient broken after updated Retrofit to Retrofit 2

Posted by: admin June 15, 2020 Leave a comment

Questions:

I got this error after updated from retrofit to retrofit2.

FATAL EXCEPTION: OkHttp Dispatcher
Process: nz.co.datacom.mars.junction, PID: 21616
java.lang.NoSuchMethodError: No virtual method
log(Ljava/lang/String;)V in class Lokhttp3/internal/Platform; or its
super classes (declaration of ‘okhttp3.internal.Platform’ appears in
/data/app/nz.co.datacom.mars.junction-1/base.apk)
at
okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109)
at
okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:157)
at
okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.access$100(RealCall.java:30)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

The libs version I used in the app are:

    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
    compile 'com.squareup:otto:1.3.7'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

Not really sure if it is because of the okhttp version or not, but I tried few different version between 2.3.0 and 3.4.0 and none of them work.

Any ideas? Thanks in advance.

How to&Answers:

Had the same problem as you.

First of all, throw out:

compile 'com.squareup.okhttp3:okhttp:3.2.0'

Retrofit has okhttp3 as it’s dependency so it is not needed. Moreover, when you check here Retrofit 2.1.0 depends on okhttp 3.3.0, so there might be a conflict. Remember, that the final dependency version is the one you write in gradle file. If you don’t write it, then Gradle automatically resolves it and fetches the right version.

There is also a newer version of Logging Interceptor and urlconnection:

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'

Hope that solves the problem.

Answer:

In addition, the most important thing to understand the fix is to ensure that okhttp3 dependencies have all the same version number as stated here by collaborators