Flutter应用启动崩溃并卡在Gradle构建阶段求助
问题解决:Flutter添加Firebase配置后应用崩溃+Gradle构建无进展
问题背景
此前项目运行正常,在android目录的build.gradle中添加Firebase相关的buildscript代码块后,出现以下问题:
- 模拟器中应用弹出后无报错直接崩溃
- Gradle构建持续无进展
- 偶尔提示Kotlin版本与二进制版本不匹配
已尝试flutter pub upgrade、flutter clean等操作,当前使用Flutter稳定版3.24.3,flutter doctor结果如下:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.22631.4317], locale pt-PT) [√] Windows Version (Installed version of Windows is version 10 or higher) [!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) X cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/to/windows-android-setup for more details. [√] Chrome - develop for the web [!] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.3) X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components: MSVC v142 - VS 2019 C++ x64/x86 build tools - If there are multiple build tool versions available, install the latest C++ CMake tools for Windows Windows 10 SDK [!] Android Studio (not installed) [√] IntelliJ IDEA Ultimate Edition (version 2023.2) [√] VS Code (version 1.94.2) [√] Connected device (4 available) [√] Network resources
解决方案
1. 修复build.gradle语法错误
你提供的build.gradle代码末尾存在语法缺失:tasks.register("clean", Delete)的闭包未闭合,导致Gradle解析失败。修改后完整代码如下:
buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.1.2' // START: FlutterFire Configuration classpath 'com.google.gms:google-services:4.4.2' // Required for Firebase // END: FlutterFire Configuration 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. 修复Android工具链问题
flutter doctor提示的两个环境问题会影响Android构建:
- 安装缺失的cmdline-tools:执行命令
sdkmanager --install "cmdline-tools;latest" - 接受SDK许可证:执行命令
按提示输入flutter doctor --android-licensesy接受所有许可证。
3. 对齐Kotlin与Gradle插件版本
Android Gradle Plugin(AGP)8.1.2与Kotlin 1.9.0存在兼容性适配问题,建议将Kotlin版本调整为AGP官方推荐的1.8.20:
修改buildscript中的版本配置:
ext.kotlin_version = '1.8.20'
同时确保app模块的build.gradle中使用的Kotlin版本与根目录一致(若app模块有单独指定)。
4. 重新清理构建
完成上述修改后,执行以下命令重新构建项目:
flutter clean flutter pub get flutter run
内容的提问来源于stack exchange,提问作者Jaime Ferreira
相关产品推荐
相关产品推荐

