Flutter项目构建报错:Kotlin Gradle插件版本不符及任务创建失败
错误1:Kotlin Gradle插件版本不兼容
FAILURE: Build failed with an exception.
- What went wrong:
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
The following dependencies do not satisfy the required version:
project ':pdf_merger' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.31
原因
项目依赖的pdf_merger插件使用的Kotlin Gradle插件版本(1.4.31)低于Android Gradle插件(AGP 7.1.2)要求的最低版本(1.5.20),导致版本不兼容。
解决方案
在**app模块的build.gradle**中添加版本强制配置,让所有子模块统一使用你指定的Kotlin版本:
android { // ... 保留原有配置 // 添加以下内容 configurations.all { resolutionStrategy { force "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21" force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21" } } }
错误2:构建任务路径冲突
Multiple build operations failed.
Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
Could not create task ':url_launcher_android:generateDebugUnitTestConfig'.
Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
this and base files have different roots: D:\App_Dev\flutter_projects\diagnosis\build\path_provider_android and C:\Users\ziadg\AppData\Local\Pub\Cache\hosted\pub.dev\path_provider_android-2.2.4\android.
原因
Flutter插件的本地构建输出路径与Pub缓存路径存在冲突,导致Gradle无法正常创建构建任务。
解决方案
- 执行Flutter清理命令清除本地构建缓存:
flutter clean
- 删除项目根目录下的
.gradle文件夹和app/build文件夹 - 重新获取项目依赖:
flutter pub get
- 重启Android Studio后重新构建项目
修改后的app模块build.gradle参考代码
plugins { id "com.android.application" id 'com.google.gms.google-services' id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" } def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } android { namespace "com.example.diagnosis" compileSdk flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } // 新增版本统一配置 configurations.all { resolutionStrategy { force "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21" force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21" } } defaultConfig { applicationId "com.example.diagnosis" minSdkVersion 21 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' }
内容的提问来源于stack exchange,提问作者Ziad Ghanem

