如何修复Android Studio中‘Not targeting the latest version of Android’警告
问题描述
我把targetSdkVersion和compileSdkVersion更新到33时,发现compileSdkVersion已被废弃,于是改用compileSdk 33。但在模拟器运行应用时收到以下警告:
"Not targeting the latest versions of Android; compatibility modes apply. Consider testing and updating this version. Consult the android.os.Build.VERSION_CODES javadoc for details."
当前模块级build.gradle代码如下:
android { compileSdk 33 defaultConfig { applicationId "com.wastefreemail.wfmconnect" minSdkVersion 16 targetSdkVersion 33 versionCode 17 versionName "1.3.4" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } namespace 'com.wastefreemail.wfmconnect' } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
解决方法
这个警告是因为当前使用的compileSdk和targetSdkVersion不是Android Gradle Plugin(AGP)识别的最新稳定版本,或项目存在版本不匹配情况,按以下步骤处理:
同步更新到最新稳定Android SDK版本
将compileSdk和targetSdkVersion统一替换为当前最新的稳定Android版本(例如当前最新稳定版为34):android { compileSdk 34 defaultConfig { // 保留其他原有配置 targetSdkVersion 34 // 保留其他原有配置 } }更新Android Gradle Plugin(AGP)版本
确保AGP版本支持最新SDK,在项目级build.gradle或settings.gradle中更新到最新稳定版(例如8.2.0):// 项目级build.gradle示例 plugins { id 'com.android.application' version '8.2.0' apply false id 'com.android.library' version '8.2.0' apply false }同步更新依赖库版本
将核心依赖更新到适配最新SDK的稳定版本:dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.7.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.5' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }清理并同步项目
点击Android Studio右上角的「Sync Project with Gradle Files」同步配置,再依次执行「Build > Clean Project」和「Build > Rebuild Project」完成构建。
完成以上操作后,警告即可消除。若仍有问题,检查第三方依赖是否适配最新SDK版本,同步更新即可。
内容的提问来源于stack exchange,提问作者stefan sanders

