Expected Android API level 21+ but was 19 while using Retrofit 2, OkHttp3

okhttp
retrofit
retrofit android
okhttpclient api
okhttpclient expected android api level 21 but was 19
okhttp github
retrofit library in android
okhttp api

I'm using Retrofit 2 and OkHttp3 to data from server and I get error while using Min_SDK 17 and my device's API is 17 also

I tried this answer :How to fix Expected Android API level 21+ but was 19 in Android

Also I tried this answer https://github.com/square/okhttp/issues/4597#issuecomment-461204144

but I get the same error .

my Gradle.Build class

    apply plugin: 'com.android.application'

     android {
compileSdkVersion 28
defaultConfig {
    applicationId "some"
    minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}
compileOptions {
    targetCompatibility = "8"
    sourceCompatibility = "8"
}
lintOptions {
    checkReleaseBuilds false
    // Or, if you prefer, you can continue to check for errors in release builds,
    // but continue the build even when errors are found:
}
 }

 dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v13:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0"
implementation "com.squareup.okhttp3:okhttp:3.11.0"
implementation "com.squareup.okhttp3:okhttp-urlconnection:3.11.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'net.alhazmy13.hijridatepicker:library:2.0.2'
implementation group: 'com.github.msarhan', name: 'ummalqura-calendar', version: '1.1.9'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-places:16.1.0'
implementation 'com.github.jaiselrahman:FilePicker:1.0.2'
implementation 'com.github.bumptech.glide:glide:4.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.13.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.google.android.gms:play-services-auth-api-phone:16.0.0'

        annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   }

my API Client

    public class APIClient {

private static Retrofit retrofit;
private static OkHttpClient okHttpClient;

public static Retrofit getInstanceRetrofit(){

    if(okHttpClient==null) {
        initOkHttp();
    }
    if(retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
        return retrofit;

}

  private static void initOkHttp() {
    int REQUEST_TIMEOUT = 60;
    OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
            .connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    httpClient.addInterceptor(interceptor);

    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(@NonNull Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder requestBuilder = original.newBuilder()
                    .addHeader("Accept", "application/json")
                    .addHeader("Content-Type", "application/json");
            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });

    okHttpClient = httpClient.build();
}
  }

my Logs

    1-03 06:09:07.850 2827-2827/some
   E/AndroidRuntime: FATAL EXCEPTION: main
Process: some, PID: 2827
java.lang.ExceptionInInitializerError
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235)
    at some.Network.APIClient.initOkHttp(APIClient.java:40)
    at some.Network.APIClient.getInstanceRetrofit(APIClient.java:25)
    at some.MvpImplementation.DropdownImpInteractor.getDropdowns(DropdownImpInteractor.java:18)
    at some.MvpImplementation.DropdownImpPresenter.requestDropdowns(DropdownImpPresenter.java:21)
    at some.Views.LoginActivity.onCreate(LoginActivity.java:87)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    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:5001)
    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:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    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)
    at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:296) 
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:262) 
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:235) 
    at some.Network.APIClient.initOkHttp(APIClient.java:40) 
    at some.Network.APIClient.getInstanceRetrofit(APIClient.java:25) 
    at some.MvpImplementation.DropdownImpInteractor.getDropdowns(DropdownImpInteractor.java:18) 
    at some.MvpImplementation.DropdownImpPresenter.requestDropdowns(DropdownImpPresenter.java:21) 
    at some.Views.LoginActivity.onCreate(LoginActivity.java:87) 
    at android.app.Activity.performCreate(Activity.java:5231) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
    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:5001) 
    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:785) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    at dalvik.system.NativeStart.main(Native Method)

The problem is that you have added OkHttp dependency twice.

In your build.gradle you have:

dependencies {
    ...
    implementation "com.squareup.okhttp3:okhttp:3.11.0"
    ...
    implementation "com.squareup.okhttp3:okhttp:3.13.1"
}

Starting with version 3.13.0 they removed support for Android < 5. You just need to delete the

implementation "com.squareup.okhttp3:okhttp:3.13.1"

line and it should work fine

java.lang.IllegalStateException: Expected Android API level 21+ but , java.lang.IllegalStateException: Expected Android API level 21+ but was 19 # 5162. Closed. fishsoft opened this issue on Jun 2, 2019 � 3 comments. Closed� 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.

If you guys have still crash on Android 4.0 and above, here is quick solution:

implementation("com.squareup.retrofit2:retrofit:2.7.1")

implementation("com.squareup.okhttp3:okhttp:3.12.8") {
force = true
}

implementation "com.squareup.okhttp3:logging-interceptor:3.12.8"

Above code tested and verified on Android 4.4 Thanks

Meet Google Play's target API level requirement, Earlier than Android 5.0 (API level 21); Earlier than Android 6.0 (API level 23); Earlier Configuring your app to target a recent API level ensures that users can benefit from Handles Doze with expected results and no errors. Earlier than Android 5.0 (API level 21) See the respective Behavior Changes page for each of the following releases to ensure your that your app has accounted for changes introduced in these releases: Android 5.0 (API level 21). Android 4.4 (API level 19). Android 4.1.x (API level 16). Continue by following the instructions in the next section.

In your gradle file you have:

implementation "com.squareup.okhttp3:okhttp:3.11.0"

The docs say:

https://square.github.io/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, 2020.

So you could try:

implementation "com.squareup.okhttp3:okhttp:3.12.0"

CSDN博客_expected android api level 21+ but was 19, 版权声明:本文为延成原创文章,转载请标明出处问题复现Caused by: java.lang. IllegalStateException: Expected Android API level 21+ but was 19� Expected Android API level 21+ but was 19 #61. ttxz opened this issue Jun 11, 2019 · 1 comment Comments. Copy link Quote reply ttxz commented Jun 11, 2019

OkHttp Caused by: java.lang.IllegalStateException: Expected , IllegalStateException: Expected Android API level 21+ but was 19 at okhttp3. internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:238) at� Expected Android API level 21+ but was 19 #63. Closed Jonny798 opened this issue Jun 16, 2019 · 2 comments Closed Expected Android API level 21+ but was 19 #63.

new OkhttpClient() crash on android 4.4 - okhttp, IllegalStateException: Expected Android API level 21+ but was 19 , it should be a version of the problem, gookhttpI checked that I didn't read the introduction� Existing Xamarin.Android projects that have the Code shrinker set to r8 and that have the Minimum Android version set to at least Android 5.0 Lollipop (API level 21) will now ignore any custom class lists provided via the MultiDexMainDexList build action. The build will output a new warning in this case:

OKhttp reported error in java.lang.ExceptionInInitializerError on 4.x , Expected Android API level 21+ but was 19 while using Retrofit 2, OkHttp3. 发表于 2019-11-03 11:37:35. 活跃于 2019-11-04 08:44:27. 查看28 次. java android� A user whose device is running Android 1.6 (API Level 4) downloads and installs the app. After a few weeks, the user receives an over-the-air system update to Android 2.0 (API Level 5). After the update is installed, the system checks the application's maxSdkVersion and successfully re-validates it. The application functions as normal.

Comments
  • use a lower version of retrofit
  • nicely spotted :-)
  • Thank you for your answer , I solved it by @MatPag Answer