Flutter项目Android端Gradle构建失败求助
修复Flutter Android Gradle构建失败问题
先梳理下你当前配置里的几个关键问题:
- 错用了
gradle-kotlin插件,应该用标准的Android Gradle插件 - Kotlin版本2.0.20太高,和当前Flutter版本不兼容
- 没正确引入Flutter Gradle插件,替代过时的
app_plugin_loader - 硬编码了版本号,没用到Flutter配置的数值
直接上修复后的配置文件
1. 修改android/build.gradle
buildscript { ext.kotlin_version = '1.9.20' repositories { google() mavenCentral() } dependencies { // 替换成标准Android Gradle插件,版本和Flutter兼容 classpath 'com.android.tools.build:gradle:8.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // 添加官方Flutter Gradle插件,替代过时的app_plugin_loader classpath 'dev.flutter:flutter-gradle-plugin:2.0.0' } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } tasks.register("clean", Delete) { delete rootProject.buildDir }
2. 修改app/build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' // 引入Flutter Gradle插件,解决过时问题 id 'dev.flutter.flutter-gradle-plugin' // id 'com.google.gms.google-services' } def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') ?: '1' def flutterVersionName = localProperties.getProperty('flutter.versionName') ?: '1.0' def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { namespace 'com.semenov.canada' compileSdkVersion 34 defaultConfig { applicationId "com.semenov.canada" minSdkVersion 21 targetSdkVersion 34 // 用Flutter提供的版本号,不要硬编码 versionCode flutterVersionCode.toInteger() versionName flutterVersionName manifestPlaceholders = [applicationName: "com.semenov.canada"] } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null storePassword keystoreProperties['storePassword'] } } buildTypes { release { signingConfig signingConfigs.release } } } // 恢复Flutter配置,指向你的项目根目录 flutter { source '../' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
最后执行这些操作
- 终端运行
flutter clean清空旧缓存 - 打开Android Studio点击Gradle同步按钮,重新同步项目
- 如果还是有问题,执行
flutter upgrade升级到最新稳定版Flutter SDK
内容的提问来源于stack exchange,提问作者SKV
相关产品推荐
相关产品推荐

