67

How is it possible that I get this message? It does not make any sense. I'm using com.squareup.retrofit2:retrofit:2.9.0

     Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 30
    at okhttp3.internal.platform.AndroidPlatform$Companion.buildIfSupported(AndroidPlatform.kt:370)
    at okhttp3.internal.platform.Platform$Companion.findPlatform(Platform.kt:204)
    at okhttp3.internal.platform.Platform$Companion.access$findPlatform(Platform.kt:178)
    2020-09-16 12:37:07.645 6480-6480/lv.ltt.gasogmp.dev_v3 E/AndroidRuntime:     at 
    okhttp3.internal.platform.Platform.<clinit>(Platform.kt:179)
Max
  • 739
  • 2
  • 8
  • 23
Oridadedles
  • 831
  • 1
  • 6
  • 8
  • Possible Duplicate of https://stackoverflow.com/questions/56818660/how-to-fix-expected-android-api-level-21-but-was-19-in-android – RAINA Sep 16 '20 at 09:50
  • 4
    That question is about when SDK INT is actually below 21. But i have SDK INT 30 ! @RAINA – Oridadedles Sep 16 '20 at 09:53
  • It also works with SDK 28 – Oridadedles Sep 16 '20 at 10:02
  • @Oridedles may be the Library has not add its support to SDK 30 yet..I am not sure .Cant you downgrade and check the latest SDK version it works? and try to continue with that may be – RAINA Sep 16 '20 at 11:31
  • Add an explicit dependency on OkHttp 4.9.0. – Jesse Wilson Sep 16 '20 at 12:10
  • @JesseWilson yes sure i could do that, anyway they should improve exception message because it is not entirely correct and confusing – Oridadedles Sep 16 '20 at 13:07
  • The bug has been fixed a while ago, but because you are on an older version you got hit by it still. Generally OkHttp does a lot of work to hide the platform differences for a lot of different platforms and versions. When these change, we need to update the checks we use and rely on client upgrading to get the newest detection logic. – Yuri Schimke Sep 18 '20 at 20:30
  • Have a similar problem with 'com.github.bumptech.glide:okhttp3-integration:4.11.0'. It looks like the library doesn't support API 30. – Darari Nur Amali Sep 28 '20 at 07:01
  • I had this problem too and @JesseWilson your comment solved it, you should make it an answer to the question so that it can be accepted. – Craig Grummitt Oct 06 '20 at 09:30

4 Answers4

105

Add an explicit dependency on OkHttp 4.9.0. Or whatever is newest at the time you read this.

This bug is embarrassing but it's been fixed for so long that you shouldn’t be encountering it in new code.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Fixed for me. I also had to update some okio usages (access 'source' and 'buffed' directly instead of calling their methods) – Rodrigo Calderano Barbacovi Oct 07 '20 at 18:07
  • FYI, latest is 4.9.1. Look here for the latest at the time of reading this: https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp – Joshua Pinter Jul 07 '21 at 18:17
  • 3
    Also, for anybody still using version 3.x of OkHttp, updating to 3.14.7 or higher resolves the error, see this changelog entry: https://square.github.io/okhttp/changelog_3x/#version-3147 (Latest version at time of writing is 3.14.9) – Klemens Zleptnig Jan 31 '22 at 17:56
64

Just Add the Dependency implementation 'com.squareup.okhttp3:okhttp:4.9.2'

PS - Check the latest version here before you add the dependency https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
Hitesh Kushwah
  • 1,351
  • 13
  • 23
3

Add implementation 'com.squareup.okhttp3:okhttp:4.9.2' inside android\app\build.gradle

dependencies {
    ........
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.android.support:multidex:1.0.3'   
    implementation 'com.squareup.okhttp3:okhttp:4.9.2' 
    ..........
}
Moumit
  • 8,314
  • 9
  • 55
  • 59
1

Upgrage Okhttp3 or retrofit version

Latest Okhttp3 version is 4.9.2
Latest Retrofit version is 2.9.0

implementation 'com.squareup.okhttp3:okhttp:4.9.2' 
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
Shomu
  • 2,734
  • 24
  • 32