我们建议使用较新的 Android Gradle 插件来使用 compileSdk = 33

We recommend using a newer Android Gradle plugin to use compileSdk = 33

提问人:Kotlin Learner 提问时间:7/26/2022 最后编辑:TylerHKotlin Learner 更新时间:11/18/2023 访问量:95735

问:

如何更新到最新版本?我找不到最新的gradle-plugin

build.gradle(示例应用)

buildscript {
    ext {
        kotlin_version = '1.6.10'
        kotlin_serializtion_version = '1.5.21'
        kotlin_coroutines_version = "1.6.0"
        moshiVersion = "1.13.0"
        retrofit2_version = "2.9.0"
        okhttp3_version = "4.9.0"
        mockk_version = "1.12.2"
        picassoVersion = "2.71828"
        lifecycle_version = "2.5.0"
        koin_version = "3.2.0"
        barcode_scanner_version = "16.1.1"
        camerax_version = "1.0.0"
        camera_view_version = "1.0.0-alpha22"
        stripe_version = "19.1.0"
        jacoco_version = "0.8.7"
        room_version = "2.4.1"
        glide_version = "4.12.0"
        json_version = "20180813"
        kotlin_reflect_version = "1.6.10"
        compose_version = '1.2.0-rc03'
        espresso_version = '3.4.0'
        core_testing_version = '2.1.0'
        pdf_view_version = '3.2.0-beta.1'
        appboy_version = '21.0.0'
        mp_chart_version = "3.1.0"
        activity_compose = "1.5.0"
        compose_material3 = "1.0.0-alpha14"
    }

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_serializtion_version"
        classpath 'com.google.firebase:firebase-appdistribution-gradle:3.0.0'
    }
}

plugins {
    id "org.sonarqube" version "3.3"
}
apply from: "${rootDir}/config/sonarqube/sonarqube.gradle"

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release' }
        maven { url 'https://mobile-sdk.jumio.com' }
        maven { url "https://appboy.github.io/appboy-android-sdk/sdk" }
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip

build.gradle(:应用)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlinx-serialization'
apply plugin: 'com.google.firebase.appdistribution'

android {
    compileSdkVersion 33

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 21
        targetSdkVersion 33
        .....
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    sourceSets {
        test {
            java.srcDirs =
                [
                    "${project.projectDir}/src/test/java"
                ]
        }
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    buildFeatures {
        viewBinding true
        compose true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "1.2.0"
        kotlinCompilerVersion kotlin_version
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.browser:browser:1.4.0'
    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation "androidx.fragment:fragment-ktx:1.5.0"
    implementation 'com.google.android.play:core:1.10.3'
    implementation 'com.google.android.material:material:1.5.0'

    // kotlin atomic
    implementation 'org.jetbrains.kotlinx:atomicfu:0.17.1'

    //Kotlin extensions library for Play Core
    implementation 'com.google.android.play:core-ktx:1.8.1'

    //Android Kotlin extensions
    implementation "androidx.core:core-ktx:1.8.0"

    //Unit test
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-inline:3.4.0'
    androidTestImplementation 'org.mockito:mockito-android:3.4.0'
    testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
    testImplementation 'androidx.arch.core:core-testing:2.1.0'
    testImplementation "io.mockk:mockk:${mockk_version}"
    testImplementation "org.json:json:$json_version"

    //Moshi (JSON parser)
    implementation "com.squareup.moshi:moshi-kotlin:$moshiVersion"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion"

    //Retrofit2
    implementation "com.squareup.retrofit2:retrofit:$retrofit2_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit2_version"

    //Paging 3
    implementation "androidx.paging:paging-runtime-ktx:3.1.1"

    //Okhttp3
    implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3_version"
    testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp3_version"

    //Kotlin Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_coroutines_version"

    //ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"

    //HTML parser
    implementation 'org.jsoup:jsoup:1.12.1'

    //Payments
    implementation 'com.google.android.gms:play-services-wallet:19.1.0'
    implementation "com.stripe:stripe-android:$stripe_version"

    //Firebase
    implementation platform('com.google.firebase:firebase-bom:27.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-crashlytics-ktx'
    implementation 'com.google.firebase:firebase-config-ktx'
    implementation "com.google.firebase:firebase-messaging"
    implementation "com.google.firebase:firebase-core"

    //Pager indicator
    implementation 'me.relex:circleindicator:2.1.4'

    //Animations
    implementation 'com.airbnb.android:lottie:3.2.2'
    implementation 'pl.bclogic:pulsator4droid:1.0.3'

    //Loading images
    implementation "com.github.bumptech.glide:glide:$glide_version"
    kapt "com.github.bumptech.glide:compiler:$glide_version"

    //View container which can wrap content
    implementation 'com.google.android:flexbox:2.0.1'

    //Phone Authentication
    implementation 'com.google.android.gms:play-services-auth:20.1.0'
    implementation 'com.google.android.gms:play-services-auth-api-phone:18.0.1'
    implementation 'com.googlecode.libphonenumber:libphonenumber:8.7.0'

    //Video player
    implementation 'com.google.android.exoplayer:exoplayer:2.11.3'

    //Dependency injection - Koin AndroidX Scope features
    implementation "io.insert-koin:koin-android:$koin_version"
    implementation "io.insert-koin:koin-androidx-workmanager:$koin_version"
    testImplementation "io.insert-koin:koin-test-junit4:$koin_version"
    testImplementation "io.insert-koin:koin-test:$koin_version"

    //WorkManager
    implementation "androidx.work:work-runtime-ktx:2.7.1"

    // App Startup
    implementation "androidx.startup:startup-runtime:1.1.0"

    //Progress button extension
    implementation 'com.github.razir.progressbutton:progressbutton:2.1.0'

    //PDF Viewer
    implementation "com.github.barteksc:android-pdf-viewer:$pdf_view_version"

    //Amplitude
    implementation 'com.amplitude:android-sdk:2.25.2'

    //Salesforce chat
    implementation "com.salesforce.service:chat-ui:4.2.2"

    //Jumio
    implementation "com.jumio.android:core:3.9.2@aar"   // Jumio Core library
    implementation "com.jumio.android:nv:3.9.2@aar"     // Netverify library
    implementation "com.jumio.android:nv-barcode:3.9.2@aar"   // Barcode scanning
    implementation "com.jumio.android:nv-mrz:3.9.2@aar" // MRZ scanning

    //AppsFlyer
    implementation 'com.appsflyer:af-android-sdk:6.2.0'
    implementation 'com.android.installreferrer:installreferrer:2.2'

    // Code Scanner & Camera
    implementation "com.google.mlkit:barcode-scanning:$barcode_scanner_version"
    implementation "androidx.camera:camera-camera2:$camerax_version"
    implementation "androidx.camera:camera-lifecycle:$camerax_version"
    implementation "androidx.camera:camera-view:$camera_view_version"

    // Braze (marketing push notifications)
    implementation "com.appboy:android-sdk-ui:$appboy_version"

    // Room
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    androidTestImplementation "androidx.room:room-testing:$room_version"
    implementation "androidx.room:room-runtime:$room_version"

    // Compose
    implementation "androidx.compose.material3:material3:$compose_material3"
    implementation "androidx.compose.runtime:runtime:$compose_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.foundation:foundation:$compose_version"
    implementation "androidx.compose.foundation:foundation-layout:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.activity:activity-compose:$activity_compose"
    androidTestImplementation "androidx.compose.ui:ui-test:$compose_version"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    // MP Charts
    implementation "com.github.PhilJay:MPAndroidChart:v$mp_chart_version"

    // Testing dependencies
    androidTestImplementation "androidx.arch.core:core-testing:$core_testing_version"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso_version"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_version"

    // Modules
    lintChecks project(':lint')

    // Miscellaneous
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_reflect_version"
    //Memory leak analysis - should only run in dev builds (devqaImplementation)
    //devqaImplementation 'com.squareup.leakcanary:leakcanary-android:2.2'
}

收到此警告消息:

我们建议使用较新的 Android Gradle 插件来使用 compileSdk = 33

此 Android Gradle 插件 (7.2.1) 已过测试,最高可达 compileSdk = 32

可以通过将此警告添加到此项目的 gradle.properties 来抑制android.suppressUnsupportedCompileSdk=33

构建将继续进行,但强烈建议您更新项目,以使用已通过 compileSdk = 33 测试的更新的 Android Gradle 插件

我已经更新了所有内容。我错过了什么?

Android Kotlin kotlin-gradle-plugin

评论


答:

18赞 Daniel Schröder 7/26/2022 #1

由于此消息只是一个警告,如果您的构建工作正常,我建议您禁止显示警告,暂时不要更新您的 Android Gradle 插件。升级此插件可能会给您带来更多的麻烦而不是好处。

Android Gradle 插件已于 maven.google.com 发布到 Google 的 Maven 存储库。

该插件的列表及其所有版本可以在这里找到。您可以选择该列表的一个版本并测试您的内部版本。

只需将文件的以下行更新为所需的版本:build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:<NEW_VERSION_HERE>'
    ...
}

请记住,您可能需要将 Android Studio 更新到 Beta 版或 Canary 频道,才能毫无问题地使用该插件的较新版本。

-2赞 Nuno Jesus 10/29/2022 #2

以下更改对我有用:

ctrl+alt+shift+S

在 ProjectStructure 中,将

模块 / 属性 / 编译 SDK 版本 / 33

模块 / 默认配置 / 目标 SDK 版本 / 33

模块 / 默认配置 / 最低 SDK 版本 / 33

然后选择具有 API 33 的模拟器,例如:Pixel 6 API 33

评论

5赞 Zohaib Ahmad 1/17/2023
为什么要更改最低 SDK 版本。
1赞 nasser 8/16/2023
将 min SDK 更改为 33,是一个非常糟糕的“解决方案”
6赞 Antonio Guerra 1/31/2023 #3

更新 build.gradle(项目)中的插件

id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false

它对我有用。

9赞 Osama Al-daeri 5/4/2023 #4

添加到您的文件:android.suppressUnsupportedCompileSdk=33gradle.properties

评论

0赞 ravi 7/3/2023
但是,禁止显示警告并不是解决方案。是吗?
0赞 TylerH 11/18/2023
@ravi 如果你不在乎它在警告你什么,这是一个解决方案。
0赞 ravi 11/22/2023
@TylerH同意,但使用最新版本是我们应该做的,不是吗?compileSdk
-2赞 Ahmad Habib 9/22/2023 #5

我在 React Native 中遇到了它,这个解决方案帮助了我:

您收到的消息是建议将 Android Gradle 插件更新到已使用 compileSdkVersion 33 测试的较新版本。虽然您的 build 将继续使用您当前的设置,但更新 Android Gradle 插件通常是确保兼容性并利用任何改进或错误修复的好做法。

要更新您的 Android Gradle 插件,您可以按照以下步骤操作:

1:在 Android Studio 中打开您的项目:

2:查看您当前的 Gradle 插件版本:

在项目的(项目级别)中,您将找到如下部分: 启动 Android Studio 并打开您的 Android 项目。build.gradle

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.0" // Your current version
        // ...
    }
}

检查在依赖项下的类路径行中指定的版本号。在上面的示例中,它是“4.2.0。"

3:更新 Gradle 插件版本:

您可以将版本更新到与 compileSdkVersion 33 兼容的较新版本。您可以通过访问 Gradle 插件发行说明或查看 Gradle 网站上的 Gradle 插件页面来查找最新版本。

将现有版本替换为新版本。例如:

classpath "com.android.tools.build:gradle:7.0.0" // Newer version

4:同步 Gradle 文件:

更新 Gradle 插件版本后,Android Studio 会提示您同步 Gradle 文件。单击“立即同步”按钮以启动同步过程。

5:查看并解决兼容性问题:

同步后,查看生成输出并解决由于更新而可能出现的任何兼容性问题或错误。您可能需要根据需要调整其他依赖项或代码。

6:测试您的应用:

全面测试您的应用,确保其与更新后的 Gradle 插件和 .compileSdkVersion 33

7:构建和发布:

一旦你确信你的应用按预期工作,你就可以继续构建和发布你的应用,如上一个回复中所述。

更新 Android Gradle 插件是使您的 Android 项目保持最新并确保与最新 Android SDK 版本兼容的重要步骤。它还可以帮助您利用 Android 开发环境中的新功能和改进。