Flutter应用构建失败:compileSdk版本不兼容问题求助
问题描述
我在尝试运行Flutter应用时,持续遭遇构建失败的异常,核心报错信息如下:
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ':app:checkDevDebugAarMetadata'.A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
24 issues were found when checking AAR metadata:
- Dependency 'androidx.fragment:fragment-ktx:1.7.1' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs.
:app is currently compiled against android-33.
Recommended action: Update this project to use a newer compileSdk of at least 34, for example 35.
Note that updating a library or application's compileSdk (which allows newer APIs to be used) can be done separately from updating targetSdk (which opts the app in to new runtime behavior) and minSdk (which determines which devices the app can be installed on).- Dependency 'androidx.fragment:fragment:1.7.1' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs.
:app is currently compiled against android-33.
Recommended action: Update this project to use a newer compileSdk of at least 34, for example 35.
...(剩余22个类似依赖报错略)
但矛盾的是,我已经在android/app/build.gradle.kts中设置了compileSdk = 35,却依然被提示当前编译版本是33。以下是我的三个Gradle配置文件内容:
1. android/build.gradle.kts
allprojects { repositories { google() mavenCentral() } } val newBuildDir: Directory = rootProject.layout.buildDirectory.dir("../../build").get() rootProject.layout.buildDirectory.value(newBuildDir) subprojects { val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name) project.layout.buildDirectory.value(newSubprojectBuildDir) } subprojects { project.evaluationDependsOn(":app") } tasks.register<Delete>("clean") { delete(rootProject.layout.buildDirectory) }
2. settings.gradle.kts
pluginManagement { val flutterSdkPath = run { val properties = java.util.Properties() file("local.properties").inputStream().use { properties.load(it) } val flutterSdkPath = properties.getProperty("flutter.sdk") require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" } flutterSdkPath } includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } } plugins { id("dev.flutter.flutter-plugin-loader") version "1.0.0" id("com.android.application") version "8.7.0" apply false id("org.jetbrains.kotlin.android") version "1.8.22" apply false } include(":app")
3. android/app/build.gradle.kts
plugins { id("com.android.application") id("kotlin-android") // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins. id("dev.flutter.flutter-gradle-plugin") } android { namespace = "com.smartqualify.example" compileSdk = 35 ndkVersion = "28.1.13356709" compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } // 注:原配置内容未完整粘贴,此处为已提供部分 }
问题分析
从报错信息和配置文件来看,核心矛盾是app模块明明配置了compileSdk=35,但构建系统却识别为33,大概率是以下原因之一:
- 根目录
build.gradle.kts中的子项目配置可能意外覆盖了app模块的compileSdk设置; - Gradle缓存未更新,导致构建系统读取了旧的配置;
- 存在其他隐性配置(比如
gradle.properties或其他子项目配置)覆盖了compileSdk版本。
解决方案
尝试按照以下步骤逐一排查修复:
1. 明确统一compileSdk版本配置
在根目录的build.gradle.kts中添加全局的Android配置约束,确保所有子项目都继承正确的compileSdk版本:
subprojects { afterEvaluate { if (plugins.hasPlugin("com.android.application") || plugins.hasPlugin("com.android.library")) { extensions.configure<com.android.build.gradle.BaseExtension> { compileSdk = 35 // 可选:统一targetSdk和minSdk,也可以在app模块单独配置 // targetSdk = 35 // minSdk = 21 } } } }
2. 清理Gradle缓存并重新构建
执行以下命令彻底清理缓存,避免旧配置干扰:
# 清理Flutter和Gradle缓存 flutter clean rm -rf ~/.gradle/caches # Linux/macOS # 或在Windows上执行:rd /s /q %USERPROFILE%\.gradle\caches # 重新构建项目 flutter pub get flutter run
3. 检查是否有其他配置覆盖
查看项目中的gradle.properties文件,确认是否存在类似以下的配置,若有则删除或修改为35:
android.compileSdkVersion=33
4. 确认Flutter SDK版本兼容性
当前使用的Flutter SDK版本可能与Android Gradle Plugin 8.7.0存在兼容性问题,尝试升级Flutter到稳定版:
flutter upgrade
额外提示
如果以上步骤都无效,可以尝试临时降级部分依赖版本(比如将androidx.fragment:fragment-ktx降级到1.6.5,该版本兼容compileSdk=33),但这只是临时方案,长远来看还是建议保持compileSdk与依赖要求一致。
内容来源于stack exchange




