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

后端 未结 11 1729
甜味超标
甜味超标 2020-12-10 01:06

In my application i want get data from server, for get connect to server i used Retrofit, OkHttp.
But when running application, show me

相关标签:
11条回答
  • 2020-12-10 01:44

    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!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-10 01:50

    Android 4.4 (works for me)

    def ok_http_version = "3.11.0"
    implementation "com.squareup.okhttp3:okhttp:$ok_http_version"
    implementation "com.squareup.okhttp3:logging-interceptor:$ok_http_version"
    
    0 讨论(0)
  • 2020-12-10 01:52

    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.

    0 讨论(0)
  • 2020-12-10 02:03

    Use version 3.11.0 for api level below 21

     implementation 'com.squareup.okhttp3:okhttp:3.11.0'
     implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
    
    0 讨论(0)
  • 2020-12-10 02:03

    Note for @Anupam answer - force is deprecated if you are using Android Studio 4.0+

    https://docs.gradle.org/current/userguide/upgrading_version_5.html

    Forced dependencies Forcing dependency versions using force = true on a first-level dependency has been deprecated.

    Force has both a semantic and ordering issue which can be avoided by using a strict version constraint.

    the way ahead is to use the keyword "strictly" for dependency downgrade. refer to https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html#sec:enforcing_dependency_version

    So you should end up with some thing like the following...

      implementation ('com.squareup.okhttp3:okhttp'){ 
                version {
                    strictly '3.12.12'
                }
        }
    
    0 讨论(0)
提交回复
热议问题