Set both compileSdkVersion and targetSdkVersion to 31 in your build.gradle(app) file.
android {
compileSdkVersion 31 // <-- This
defaultConfig {
applicationId "com.example.app"
targetSdkVersion 31 // <-- and this too
// ...
}
}