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

from–https://stackoverflow.com/questions/58679774/expected-android-api-level-21-but-was-19-while-using-retrofit-2-okhttp3

Asked 
Viewed 7k times

2

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)

3 Answers

4

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

4

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

  • getting below error : javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb9570410: Failure in SSL library, usually a protocol error error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741 0x8d9f3990:0x00000000)

    Feb 3, 2021 at 9:43

  • force = true is deprecated. Use MutableVersionConstraint#strictly(String) instead

    Feb 16, 2021 at 17:56

2

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"

上一篇
下一篇