You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter升级至SDK34遇Gradle兼容问题求助

Flutter 升级 Android SDK 34 + Gradle 8.x 问题解决方案

1. 最新版 Flutter 对 Gradle 8.x 的支持

最新版 Flutter(3.16 及以上版本)完全支持 Gradle 8.x,官方已完成对应工具链的适配,不存在核心兼容性问题。

2. 经过验证的稳定配置组合

以下是能稳定运行的版本匹配:

  • Gradle 版本:8.7
  • AGP(Android Gradle Plugin)版本:8.2.2
  • Kotlin 版本:1.9.20

针对你遇到的具体错误的修复步骤

错误1:AAPT: error: resource android:attr/lStar not found

该问题源于部分依赖库未适配 Android 14(SDK 34)的新属性,修复方法:

  1. 在项目根目录的 android/build.gradle 中添加 subprojects 块,强制统一依赖版本:
subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileSdkVersion 34
                defaultConfig {
                    targetSdkVersion 34
                }
                configurations.all {
                    resolutionStrategy {
                        force 'androidx.appcompat:appcompat:1.6.1'
                    }
                }
            }
        }
    }
}
  1. 清理缓存:执行 flutter clean,删除 android/.gradle 目录后重新构建。

错误2:Error: Gradle build failed to produce an .apk file

该问题多因 Gradle 输出路径与 Flutter 预期不匹配导致,修复方法:

  1. 检查 android/app/build.gradle 的 buildTypes 配置,确保输出路径规范:
buildTypes {
    debug {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
                    output.outputFileName = "app-debug.apk"
                }
            }
        }
    }
    release {
        signingConfig signingConfigs.debug
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
                    def fileName = "app-release-${variant.versionName}.apk"
                    output.outputFileName = fileName
                }
            }
        }
    }
}
  1. 执行明确的构建命令:flutter run --debug,避免模式混淆导致路径识别错误;同时检查 local.properties 中的 flutter.sdk 路径是否指向当前使用的 Flutter 版本。

完整配置更新流程

  1. 升级 Flutter 到 3.16+(执行 flutter upgrade)。
  2. 修改 android/build.gradle 中的依赖版本:
dependencies {
    classpath 'com.android.tools.build:gradle:8.2.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20"
}
  1. 修改 gradle/wrapper/gradle-wrapper.properties 的 Gradle 下载路径:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip
  1. 执行 flutter clean → flutter pub get → flutter run。

内容的提问来源于stack exchange,提问作者aniuni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 18:14:57