添加sqflite依赖后Flutter项目Gradle编译失败求助
问题描述
每次给Flutter项目添加sqflite插件后,运行时都会触发编译错误,具体报错信息如下:
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ':sqflite_android:compileDebugJavaWithJavac'.Could not resolve all files for configuration ':sqflite_android:androidJdkImage'.
Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.Execution failed for JdkImageTransform: C:\Users\hhh25\AppData\Local\Android\Sdk\platforms\android-34\core-for-system-modules.jar.
Error while executing process C:\Program Files\Android\Android Studio\jbr\bin\jlink.exe with arguments {--module-path C:\Users\hhh25.gradle\caches\transforms-3\4a46fc89ed5f9adfe3afebf74eb8bfeb\transformed\output\temp\jmod --add-modules java.base --output C:\Users\hhh25.gradle\caches\transforms-3\4a46fc89ed5f9adfe3afebf74eb8bfeb\transformed\output\jdkImage --disable-plugin system-modules}
- 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 13s
Running Gradle task 'assembleDebug'... 15.0s
Error: Gradle task assembleDebug failed with exit code 1
已尝试的操作:
- 安装sqflite旧版本,问题仍存在
- 删除sqflite依赖后,项目可正常运行
flutter doctor -v 输出
[√] Flutter (Channel stable, 3.24.4, on Microsoft Windows [Version 10.0.19045.5011], locale en-US) • Flutter version 3.24.4 on channel stable at C:\fluterdev\flutter_windows_3.24.3-stable\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 603104015d (12 days ago), 2024-10-24 08:01:25 -0700 • Engine revision db49896cf2 • Dart version 3.5.4 • DevTools version 2.37.3 [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 35.0.0) • Android SDK at C:\Users\hhh25\AppData\Local\Android\Sdk • Platform android-35, build-tools 35.0.0 • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [X] Visual Studio - develop Windows apps X Visual Studio not installed; this is necessary to develop Windows apps. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [√] Android Studio (version 2024.2) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11) [√] VS Code (version 1.95.1) • VS Code at C:\Users\hhh25\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.100.0 [√] Connected device (4 available) • SM M215G (mobile) • RZ8R80P4K2W • android-arm64 • Android 13 (API 33) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.5011] • Chrome (web) • chrome • web-javascript • Google Chrome 130.0.6723.92 • Edge (web) • edge • web-javascript • Microsoft Edge 130.0.2849.68 [√] Network resources • All expected network resources are available.
pubspec.yaml 依赖内容
name: mabi3ati description: "A new Flutter project." publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: sdk: ^3.5.3 dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.8 sqflite: ^2.2.0 intl: ^0.19.0 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^4.0.0 flutter: uses-material-design: true
解决方案
根据报错信息和环境配置,问题核心是Java版本与Gradle、Android SDK的兼容性冲突,按以下步骤逐一排查:
切换Java版本至17
Android Studio 2024.2默认搭载的JBR 21可能与当前Gradle版本不兼容,下载OpenJDK 17后,在Android Studio中修改JDK路径:- 打开
File > Project Structure > SDK Location - 在
JDK Location处选择已下载的OpenJDK 17目录
- 打开
调整项目Gradle配置
修改项目根目录下的android/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" } }再修改
android/app/build.gradle文件:android { compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = '17' } }清理缓存并重建项目
- 执行命令
flutter clean - 删除项目根目录及
android目录下的.gradle文件夹 - 重新执行
flutter pub get和flutter run
- 执行命令
检查Android SDK Platform 34完整性
报错中提到了android-34的系统模块文件,确保SDK中正确安装该平台:- 打开Android Studio的SDK Manager
- 找到Android 13 (Tiramisu)对应的API 34,确认已安装
内容的提问来源于stack exchange,提问作者Moh Moh

