web-dev-qa-db-fra.com

Comment réparer Expected Android API level 21+ mais était de 19 en Android

Dans mon application, je veux obtenir des données du serveur, pour me connecter au serveur, j'ai utilisé Retrofit, OkHttp.
Mais lors de l'exécution de l'application, montrez-moi forcer la fermeture de l'erreur.
Dans Android api 21 + n'est pas une erreur, mais ci-dessous api 21 affichez-moi forcez la fermeture de l'erreur.

codes de classe ApiClient:

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)
    }
}

Montrez-moi une erreur pour cette ligne: .build() sur cette méthode val client = clientBuilder

J'ai ajouté ces codes dans gradle.build

compileOptions {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

mais montrez-moi encore une fois l'erreur!

Message d'erreur:

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'

Comment puis-je le réparer?

6
Jake warton

Android 4.4 (fonctionne pour moi)

def ok_http_version = "3.11.0"
implementation "com.squareup.okhttp3:okhttp:$ok_http_version"
implementation "com.squareup.okhttp3:logging-interceptor:$ok_http_version"
2
Evgen But

Merci @ https://stackoverflow.com/users/5773044/anupam Ce code fonctionne dans Android API 19:

// Note: 3.12.+ to support Android API 19
implementation 'com.squareup.okhttp3:okhttp:3.12.10'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.10'

implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.4'
implementation 'com.squareup.retrofit2:converter-gson:2.6.4'
implementation 'com.squareup.retrofit2:converter-scalars:2.6.4'
testImplementation 'com.squareup.retrofit2:retrofit-mock:2.6.4'
androidTestImplementation 'com.squareup.retrofit2:retrofit-mock:2.6.4'
1
Erlang P

Utilisez la version 3.11.0 pour le niveau api inférieur à 21

 implementation 'com.squareup.okhttp3:okhttp:3.11.0'
 implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
1
AceStan

J'étais confronté au même problème au niveau de l'API 19 lorsque j'ai ajouté ci-dessous dans le fichier build.gradle.

implementation 'com.squareup.okhttp3:okhttp:3.12.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.13.1'

Enfin, le code ci-dessous m'a aidé à résoudre ce problème:

  implementation 'com.squareup.okhttp3:okhttp:3.11.0'
  implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

J'espère que cette solution fonctionne bien pour tous.

1
Arjun G