Solutions for OkHttp, Retrofit, and Alibaba Cloud Storage that are not compatible with systems below Android 5.0

created at 12-29-2021 views: 24

Preface

When running the framework with OkHttp as the bottom layer on phones below Android5.0, a crash error will occur. The error is as follows:

java.lang.ExceptionInInitializerError
...
Caused by: java.land.IllegalStateException:Expected Android API level 21+ but was 19

The description on the official website is as follows:

Retrofit

Retrofit requires at minimum Java 8+ or Android API 21+

OkHttp

OkHttp works on Android 5.0+ (API level 21+) and on Java 8+.

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,2021.

environments

retrofit is the 2.9.0 version, which is the latest version at the time of writing

If it is compatible with 5.0 or below, you need to modify okHttp to 3.12.x version

The purpose is to modify the OkHttp version, not the Retrofit version, so as not to cause extensive changes

Note that since most of the current projects are very complex, the projects are divided into many modules, no matter how many modules, OkHttp needs to be unified to the 3.12.0 version, and the search method will be given later.

method (1)

Project dependencies (follow the directory build.gradle):

    converter_gson              : 'com.squareup.retrofit2:converter-gson:2.9.0',
    retrofit                    : 'com.squareup.retrofit2:retrofit:2.9.0',
    adapter_rxjava              : "com.squareup.retrofit2:adapter-rxjava2:2.9.0",

Module dependencies:

    api(rootProject.ext.dependencies.converter_gson){
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }
    api(rootProject.ext.dependencies.retrofit){
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }
    api(rootProject.ext.dependencies.adapter_rxjava){
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }
    implementation "com.squareup.okhttp3:okhttp:3.12.0"

method (2)

The outermost configuration of build.gradle under the app module

configurations {
    all*.exclude group:'com.google.android', module:'support-v4'
// Or be rude, there won't be any of the above problems
     all*.exclude module:'okhttp'
}
configurations.all {
      resolutionStrategy {
          force "com.squareup.okhttp3:okhttp:3.12.0"
        }
  }

The pros and cons of processing methods

The first method is complicated. However, if the previous version of OkHttp is relatively new and needs to be downgraded now, the APIs of the new and old versions are inconsistent. This can point out errors during compilation, so it is easy to modify

The second processing method is simple, but if the new and old APIs are inconsistent, an error will be reported during operation, which is not conducive to modification

How to troubleshoot

First of all, the problem is that if you know that it is a problem with OkHttp, then you must know where OkHttp is currently used:

The following two tools are mainly used:

  1. The dependency tree that comes with Android Studio:

dependency tree

Through this file, you can see which libraries and versions are in the project, but there is no way to know where the dependencies exist in this way. At this time, the second solution is needed.

  1. Android Studio plug-in Gradle View

Although Android Studio also comes with its own view of dependent branches, but it is not clear and intuitive, so here you can learn from the third-party plug-in Gradle View, which can be installed in the plug-in warehouse.

dependencies

created at:12-29-2021
edited at: 12-29-2021: