We recommend using a newer Android Gradle plugin to use compileSdkPreview = "Sv2" warning in android studio

When I am running an app in android studio using Android Sv2 Preview SDK, I am getting a warning:

Build Output

We recommend using a newer Android Gradle plugin to use compileSdkPreview = "Sv2"

This Android Gradle plugin (7.2.0-alpha04) was tested up to compileSdk = 31

This warning can be suppressed by adding android.suppressUnsupportedCompileSdk=Sv2
to this project's gradle.properties

The build will continue, but you are strongly encouraged to update your project to
use a newer Android Gradle Plugin that has been tested with compileSdkPreview = "Sv2"

build.gradle (:app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 31
    compileSdkPreview 'Sv2'
...
}
...

build.gradle (Project:...)

buildscript {
    ext {
        compose_version = '1.1.0-beta02'
        agp_version = '7.2.0-alpha04'
    }
}// Top-level build file where you can add configuration options common to all sub- 
projects/modules.
plugins {
    id 'com.android.application' version '7.2.0-alpha04' apply false
    id 'com.android.library' version '7.2.0-alpha04' apply false
    id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}

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

where can I get the correct AGP(Android Gradle plugin) version?

What is apply false in

plugins {
    id 'com.android.application' version '7.2.0-alpha04' apply false
    id 'com.android.library' version '7.2.0-alpha04' apply false
    id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}

Solution 1:

This is not a error, This is only a warning that you will only encounter in stable version of Android Studio,

Because Android Sdk 32 does not have a stable version release, it is currently in beta version. Stable version of Android Studio recommends to use stable version of Android Gradle Plugin and therefore shows a warning when you use beta version of android SDK as compileSDK,

So the best solution is to ignore the warning or downgrade the version of compileSDK to current stable version which is 31 or suppress warning by adding "android.suppressUnsupportedCompileSdk=32" to gradle.properties. However if you really want to checkout features of new Android, use Android Studio Canary which is explicitly built for beta version of Android SDK, beta version Android Gradle Plugin and latest Gradle version. if above warning also shows up in android canary with latest version of Android Gradle Plugin, it means Android Gradle Plugin for new SDK has not been released, So there is no solution until or unless Android Gradle Plugin for new SDK is released.

Solution 2:

The apply false syntax is used to tell Gradle not to apply the plugin to the current project and then use the plugins {} block without the version in subprojects' build scripts.

more details in the example given in official gradle doc