【Android Studio】An issue was found when checking AAR metadata~でアプリが実行できない場合の対処法

実機やエミュレータでアプリを実行するときに、以下のようなメッセージが表示されアプリが実行できない場合の対処方法を紹介します。
An issue was found when checking AAR metadata:
1. Dependency ‘androidx.XXX: XXX:X.X.X’ requires libraries and applications that depend on it to compile against version XX or later of the
Android APIs.

原因
コンパイルに使っている SDK のバージョンが、使用中のライブラリで要求されているバージョンに一致しないことが原因です。
解決方法は
- SDK を変更する(オススメ)
- ライブラリのバージョンを変更する
のどちらかになります。
解決方法① SDK の変更
エラーメッセージに Recommended action: Update this project to use a newer compileSdk~ と書いてある箇所があります。
ここで推奨されているバージョン(画像では 35)を覚えておいてください。

画面左側のプロジェクト構造から Gradle Scripts → build.gradle.kts (Module :app) を開きます。

8・13行目あたりにある compileSdk と targetSdk を先ほどのバージョンに変更します。
変更したら画面右上に表示される Sync Now をクリックします。

アプリを実行できるかをご確認ください。
解決方法② ライブラリのバージョンを変更する
変更が必要なライブラリを確認します。
エラーメッセージの 1. Dependency ‘ライブラリ名’ requires libraries and applications that に書いてあります(画像では android.core:core:1.15.0 )

画面左側のプロジェクト構造から Gradle Scripts → libs.versions.toml(Version Catalog) を開きます。

該当するライブラリのバージョンを古いものに変更して、右上に表示される Sync Now をクリックします。
ライブラリのバージョン番号は以下のページから確認できます。https://developer.android.com/jetpack/androidx/explorer?hl=ja

アプリを実行できるかをご確認ください。