question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on retrofit when add dependencies of okhttp

See original GitHub issue

Hello, when I am using Retrofit without Okhttp dependencies, its working fine, but when I want to add okhttp for logging to my dependencies. It show error in my logcat:

EXCEPTION: main
    Process: com.myapp., PID: 2687
    java.lang.ExceptionInInitializerError
        at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296)
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262)
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235)
        at retrofit2.Retrofit$Builder.build(Retrofit.java:580)
        at com.myapp..fragment.LoginFragment.onCreate(LoginFragment.java:73)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188)
        at android.app.Activity.performStart(Activity.java:5241)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 19
        at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238)
        at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202)
        at okhttp3.internal.platform.Platform.<clinit>(Platform.java:79)
        at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296) 
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262) 
        at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235) 
        at retrofit2.Retrofit$Builder.build(Retrofit.java:580) 
        at com.myapp.fragment.LoginFragment.onCreate(LoginFragment.java:73) 
        at android.support.v4.app.Fragment.performCreate(Fragment.java:2414) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418) 
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195) 
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078) 
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117) 
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408) 
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) 
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) 
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) 
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620) 
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1188) 
        at android.app.Activity.performStart(Activity.java:5241) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5019) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 

No problem when I using retrofit only

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

59reactions
pdapnzcommented, Feb 20, 2019

Same problem.

This is because:

OkHttp [starting from 3.13] works on Android 5.0+ (API level 21+) and on Java 8+.

https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce

The only way is use OkHttp 3.12.x if your minSDK lower 21.

P.S. It is not clear why the OkHttp developers did not raise the major version, given such major changes.

22reactions
DDihanovcommented, Feb 19, 2020

If you guys are still crashing on Android 4 using the latest 2.7.1 Retrofit version, after setting the source and target compability version to 1.8 force the old OkHttp version like this, and it should work:

implementation("com.squareup.retrofit2:retrofit:2.7.1")

implementation("com.squareup.okhttp3:okhttp:3.12.8") {
    force = true
}

implementation "com.squareup.okhttp3:logging-interceptor:3.12.8"
Read more comments on GitHub >

github_iconTop Results From Across the Web

I have error related retrofit dependency how can I resolve it?
Change the version like: implementation 'com.squareup.retrofit2:retrofit:2.9.0'.
Read more >
Typesafe HTTP Clients with OkHttp and Retrofit - Reflectoring
The result is then audited in an in-memory database for tracking purposes. Adding Retrofit dependencies. With Maven : <dependency> <groupId>com.
Read more >
Consuming APIs with Retrofit | CodePath Android Cliffnotes
Retrofit is a type-safe REST client for Android, Java and Kotlin ... If you wish to keep the @Generated annotation, you will need...
Read more >
Using Retrofit 2.x as REST client - Tutorial - Vogella.com
Add the following dependencies to your build.gradle file. ... The created OkHttp client has to be added to your Retrofit client with the...
Read more >
Upgrading to OkHttp 4 - Square Open Source
The okhttp3.internal package is not a published API and we change it frequently without warning. Depending on code in this package is bad...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found