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

重装Flutter后新建Android项目无法编译,旧项目编译耗时增加

重装Flutter后Android编译失败及旧项目编译耗时增加问题解决

问题现象

  • 重装Flutter稳定版3.19.2后,旧项目可正常编译,但编译耗时较之前大幅上升
  • 新建项目在Windows、Web平台编译无问题,Android平台编译失败,核心报错为org/jetbrains/kotlin/config/LanguageVersion
  • 删除项目的android文件夹后,通过flutter create --platform android .重建,错误依旧
  • Flutter Doctor检测所有环境均正常,但新建项目的根目录build.gradle内容异常简短

编译报错日志

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Dev\flutter_application_1\android\build.gradle' line: 13

* What went wrong:
A problem occurred evaluating root project 'android'.
> Build completed with 1 failures.
   > org/jetbrains/kotlin/config/LanguageVersion

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
Error: Gradle task assembleDebug failed with exit code 1

Exited (1).

异常的根build.gradle内容

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
}

解决方案

1. 修复根目录build.gradle配置

正常的Flutter项目根android/build.gradle需要包含buildscript块,指定gradle插件和kotlin版本,补充以下内容到文件开头:

buildscript {
    ext.kotlin_version = '1.9.20' // 与Flutter 3.19.2兼容的kotlin版本
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

补充后完整的build.gradle示例:

buildscript {
    ext.kotlin_version = '1.9.20'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

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. 匹配gradle wrapper版本

打开android/gradle/wrapper/gradle-wrapper.properties,确保distributionUrl对应gradle 8.2版本:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

3. 清理缓存并重新构建

  • 执行命令:flutter clean
  • 删除项目的android/.gradle文件夹
  • 执行:flutter pub get
  • 重新尝试编译:flutter run

针对旧项目编译耗时增加的处理

  • 启用Android Studio的gradle离线模式(路径:File > Settings > Build, Execution, Deployment > Build Tools > Gradle > 勾选Offline work)
  • 检查本地maven仓库(默认路径C:\Users\<你的用户名>\.m2\repository)是否完整,确保依赖无需重新下载
  • 执行flutter clean后重新构建,触发gradle缓存重建

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 22:52:45