How to fix Expected Android API level 21+ but was 19 in Android
Solution 1
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!
Solution 2
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
Solution 3
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.
Solution 4
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.
Solution 5
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'
}
}
Jake warton
Updated on September 30, 2020Comments
-
Jake warton over 3 years
In my application i want get data from server, for get connect to server i used Retrofit, OkHttp.
But when running application, show me force close error.
In android api 21+ is not error, but below api 21 show me force close error.ApiClient class codes:
class ApiClient constructor(private val deviceUUID: String) { private val apiServices: ApiServices init { //Gson val gson = GsonBuilder() .setLenient() .create() //Http log val loggingInterceptor = HttpLoggingInterceptor() loggingInterceptor.level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE //Http Builder val clientBuilder = OkHttpClient.Builder() clientBuilder.interceptors().add(loggingInterceptor) clientBuilder.addInterceptor { chain -> val request = chain.request() request.newBuilder().addHeader(AGENT_NAME, AGENT_VALUE).build() chain.proceed(request) } clientBuilder.addInterceptor { chain -> val request = chain.request() request.newBuilder().addHeader(X_CLIENT_VERSION, APP_VERSION_NAME).build() chain.proceed(request) } clientBuilder.addInterceptor { chain -> val request = chain.request() request.newBuilder().addHeader(UUID_NAME, deviceUUID).build() chain.proceed(request) } //Http client val client = clientBuilder .readTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS) .writeTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS) .connectTimeout(NETWORK_CONNECTIONS_TIME, TimeUnit.SECONDS) .retryOnConnectionFailure(true) .build() //Retrofit val retrofit = Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) .build() //Init apiServices apiServices = retrofit.create(ApiServices::class.java) } companion object { private var apiClient: ApiClient? = null fun getInstance(deviceUUID: String): ApiClient = apiClient ?: synchronized(this) { apiClient ?: ApiClient(deviceUUID).also { apiClient = it } } } /** * Send apiServices to ApisUseCase for initialize all of apis */ fun apisUseCase(): ApisUseCase { return ApisUseCase(apiServices) } }
Show me error for this line :
.build()
on this methodval client = clientBuilder
I added this codes into gradle.build
compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' }
but again show me error!
Error message :
java.lang.ExceptionInInitializerError at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:263) at okhttp3.OkHttpClient.<init>(OkHttpClient.java:229) at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1015) at com.app.android.data.services.ApiClient.<init>(ApiClient.kt:56) at com.app.android.ui.splash.SplashPresenterImpl.checkUpdate(SplashPresenterImpl.kt:18) at com.app.android.ui.splash.SplashActivity.onCreate(SplashActivity.kt:40) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157) 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)
Gradle.Build codes:
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'androidx.navigation.safeargs' apply plugin: 'kotlin-kapt' apply plugin: 'io.fabric' android { compileSdkVersion 28 defaultConfig { applicationId "com.app.android" minSdkVersion 16 targetSdkVersion 28 versionCode 100 versionName "2.5.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility = '1.8' targetCompatibility = '1.8' } } repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0-beta01' implementation 'androidx.core:core-ktx:1.2.0-alpha02' implementation 'com.google.android.material:material:1.0.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.recyclerview:recyclerview:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.viewpager:viewpager:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.core:core:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' //JWT decoder implementation 'com.auth0.android:jwtdecode:1.2.0' //Anko lib implementation "org.jetbrains.anko:anko-commons:0.10.8" implementation "org.jetbrains.anko:anko-design:0.10.8" //Rx implementation "io.reactivex.rxjava2:rxandroid:2.1.1" implementation "io.reactivex.rxjava2:rxjava:2.2.8" //OkHttp implementation 'com.squareup.okhttp3:okhttp:3.14.1' implementation "com.squareup.okhttp3:logging-interceptor:3.14.0" //Retrofit implementation "com.squareup.retrofit2:retrofit:2.5.0" implementation "com.squareup.retrofit2:adapter-rxjava2:2.5.0" implementation "com.squareup.retrofit2:converter-gson:2.5.0" //Gson implementation 'com.google.code.gson:gson:2.8.5' //Image implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' //Calligraphy implementation 'io.github.inflationx:calligraphy3:3.1.0' implementation 'io.github.inflationx:viewpump:1.0.0' //Navigation implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0-alpha05' implementation 'androidx.navigation:navigation-ui-ktx:2.1.0-alpha05' //Preferences lib implementation 'com.github.MrNouri:GoodPrefs:1.0' //Multiple sizes implementation 'com.intuit.sdp:sdp-android:1.0.6' // OnBoarding implementation 'com.codemybrainsout.onboarding:onboarder:1.0.4' //Alerter implementation 'com.tapadoo.android:alerter:4.0.2' //Fabric answer implementation('com.crashlytics.sdk.android:answers:1.4.7@aar') { transitive = true } //Fabric crash implementation('com.crashlytics.sdk.android:crashlytics:2.9.9@aar') { transitive = true } //Adjust implementation 'com.adjust.sdk:adjust-android:4.17.0' implementation 'com.android.installreferrer:installreferrer:1.0' //Google and firebase services implementation 'com.google.android.gms:play-services-analytics:16.0.8' implementation 'com.google.firebase:firebase-core:16.0.9' implementation 'com.google.firebase:firebase-messaging:17.6.0' //Support MultiDex implementation 'androidx.multidex:multidex:2.0.1' //Room implementation 'androidx.room:room-runtime:2.1.0' annotationProcessor 'androidx.room:room-compiler:2.1.0' //Android svg and noneOldAndroid implementation 'com.caverock:androidsvg-aar:1.3' implementation 'com.nineoldandroids:library:2.4.0' //Animations implementation 'com.daimajia.easing:library:2.0@aar' implementation 'com.daimajia.androidanimations:library:2.3@aar' } apply plugin: 'com.google.gms.google-services'
How can i fix it?