因Gradle cp_proj类缓存问题无法运行Flutter项目,求解决方案
报错信息
FAILURE: Build failed with an exception.
- 问题所在:配置根项目'android'时出现问题。
无法为构建文件'C:\Users\teris\test\android\build.gradle'打开cp_proj通用类缓存(路径:C:\Users\teris.gradle\caches\7.5\scripts\21cu7jrt5s4ct65acfwhlwy69)。
BUG! 在源单元'BuildScript'的'conversion'阶段发生异常:org.codehaus.groovy.ast.expr.TupleExpression类无法转换为org.codehaus.groovy.ast.expr.ArgumentListExpression类(两者位于加载器org.gradle.internal.classloader.VisitableURLClassLoader @1c4af82c的未命名模块中)
环境与已尝试操作
- JDK版本:18
- Gradle版本:7.5
- 已尝试:清理Gradle缓存、更换JDK版本,问题未解决
配置文件内容
android\build.gradle
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() } dependencies { classpath 'com.google.gms:google-services:4.3.10' classpath 'com.android.tools.build:gradle:7.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } plugins { // ... // Add the dependency for the Google services Gradle plugin apply plugin: 'com.google.gms.google-services' } 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 }
app\build.gradle
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') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { namespace "com.example.test" compileSdkVersion 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' } defaultConfig { applicationId "com.example.test" minSdkVersion flutter.minSdkVersion targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } plugins { id 'com.android.application' // Add the Google services Gradle plugin id 'com.google.gms.google-services' ... } dependencies { // Import the Firebase BoM implementation platform('com.google.firebase:firebase-bom:32.1.1') implementation 'com.google.firebase:firebase-analytics' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" }
解决思路
修正插件应用语法
根项目build.gradle的plugins块内不能使用apply plugin语法,需将apply plugin: 'com.google.gms.google-services'移到buildscript块之后、allprojects块之前。
app模块build.gradle中同时存在apply plugin和plugins块声明,需统一方式,建议保留plugins块并移除重复的apply plugin行。调整插件与JDK版本
- 升级Google Services插件到兼容版本(如4.4.0),确保与Gradle 7.5、AGP 7.3.0匹配。
- 降级JDK到11或8,Gradle 7.5对JDK 18的兼容性存在局限,且项目编译配置本身指定了Java 8。
彻底清理缓存
手动删除C:\Users\teris\.gradle\caches目录,以及项目根目录下的.gradle和build文件夹,重启IDE后重新构建。
内容的提问来源于stack exchange,提问作者Stephan

