How to fix Expected Android API level 21+ but was 19 in Android

For everyone who still need the library for older version of Android, you need to use the OkHttp 3.12.x branch. The latest release from the branch is 3.12.6. I've tested and confirmed the version worked on Samsung Tablet SM-T116NU (Android 19).

Here's the excerpt from OkHttp:

The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+. These platforms lack support for TLS 1.2 and should not be used. But because upgrading is difficult we will backport critical fixes to the 3.12.x branch through December 31, 2020.


OKHTTP just dropped support for Android 4

https://github.com/square/okhttp/issues/4481

You must use an older version or maybe a sperate branch for your app.

See this Medium post from Jessie Wilson for more info: https://medium.com/square-corner-blog/okhttp-3-13-requires-android-5-818bb78d07ce


According to Okhttp documentation, they dropped the support for Android 4.

The solution is to use the 3.12.x versions of Okhttp:

def okhttp_version="3.12.0"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
implementation("com.squareup.okhttp3:okhttp") {
    version { strictly "$okhttp_version" }
}

You can also read about the reason for this change in Square blog:

Why Android 5+ and Why Now?

TLS is the mechanism that makes HTTPS calls secure, private, and authenticated. OkHttp is aware of five versions: SSLv3 (1996), TLSv1 (1999), TLSv1.1 (2006), TLSv1.2 (2008), and TLSv1.3 (2018). We dropped support for SSLv3 in 2014 in response to the POODLE attack.

Now it’s time to drop both TLSv1 and TLSv1.1 and to make TLSv1.2 the Internet’s new minimum standard. On October 15 our colleagues at Google, Mozilla, Microsoft, and Apple announced that their browsers will require TLSv1.2 or better starting in early 2020.

Google added support for TLSv1.2 in Android 5.0. Oracle added it in Java 8. In OkHttp 3.13 we require that the host platform has built-in support for TLSv1.2.

What about Android 4.x?

Google’s distribution dashboard shows that ~11% of the devices that visited the Play Store in October 2018 were running Android 4.x. We’ve created a branch, OkHttp 3.12.x, to support these devices. Should we encounter any severe bugs or security problems we’ll backport the fixes and release. We plan to maintain this branch through December 31, 2020.

If you really need TLSv1.2 on Android 4.x, that’s possible! Ankush Gupta has written a thorough guide that explains how to get Google Play Services to do it. Even if you follow this process you should still use OkHttp 3.12.x with Android 4.x devices.


For minSDK lower than 21, change your OkHttp version to 3.12.12 in gradle like this -

  //OkHttp
  implementation ("com.squareup.okhttp3:okhttp:3.12.12"){
      force = true //API 19 support
  }
  implementation 'com.squareup.okhttp3:logging-interceptor:3.12.12'

It should work fine!