Android Studio升级后Gradle及依赖冲突问题求助
升级Android Studio后Gradle与Flutter构建问题解决
问题背景
当前环境:Java 17,Gradle 8.11.1,升级Android Studio后出现多类构建错误,尝试更换Gradle/Java版本后仍有不同问题。
错误信息
1. SDK处理警告
警告:SDK处理。此版本仅支持最高版本3的SDK XML文件,但检测到版本4的SDK XML文件。这可能是因为你使用的Android Studio和命令行工具版本不一致导致的。
原英文提示:
Warning: SDK processing. This version only understands SDK XML versions up to 3 but an SDK XML file of version 4 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.
2. 重复导入错误
../AppData/Local/Pub/Cache/hosted/pub.dev/carousel_slider-4.2.1/lib/carousel_slider.dart:9:1: 错误:'CarouselController' 同时从 'package:carousel_slider/carousel_controller.dart' 和 'package:flutter/src/material/carousel.dart' 导入。 import 'carousel_controller.dart'; ^^^^^^^^^^^^^^^^^^ ../AppData/Local/Pub/Cache/hosted/pub.dev/carousel_slider-4.2.1/lib/carousel_slider.dart:48:15: 错误:'CarouselController' 同时从 'package:carousel_slider/carousel_controller.dart' 和 'package:flutter/src/material/carousel.dart' 导入。 : CarouselController() as CarouselControllerImpl, ^^^^^^^^^^^^^^^^^^
3. 构建任务失败
What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
相关配置文件
项目根目录 build.gradle
buildscript { ext.kotlin_version = '1.9.21' repositories { google() mavenCentral() } dependencies { // 使用兼容的插件版本 classpath 'com.android.tools.build:gradle:8.1.0' classpath 'com.google.gms:google-services:4.3.15' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } tasks.register("clean", Delete) { delete rootProject.buildDir }
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
app模块 build.gradle
plugins { id "com.android.application" id "kotlin-android" id "dev.flutter.flutter-gradle-plugin" id 'com.google.gms.google-services' } 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') ?: '1' def flutterVersionName = localProperties.getProperty('flutter.versionName') ?: '1.0' //apply plugin: 'com.google.gms.google-services' android { namespace "com.example.kiralik_kaleci" // 只保留一个namespace compileSdkVersion 34 ndkVersion = "27.0.12077973" buildToolsVersion "35.0.0" compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "1.8" } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "com.example.kiralik_kaleci" minSdkVersion 21 targetSdkVersion 34 versionCode flutterVersionCode.toInteger() versionName flutterVersionName multiDexEnabled true } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation platform('com.google.firebase:firebase-bom:32.6.0') implementation 'com.google.firebase:firebase-analytics' implementation 'androidx.multidex:multidex:2.0.1' }
settings.gradle
pluginManagement { def flutterSdkPath = { def properties = new Properties() file("local.properties").withInputStream { properties.load(it) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" return flutterSdkPath } settings.ext.flutterSdkPath = flutterSdkPath() includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle") repositories { google() mavenCentral() gradlePluginPortal() } plugins { id "dev.flutter.flutter-gradle-plugin" version "1.0.0" apply false } } plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version '8.1.0' apply false id "org.jetbrains.kotlin.android" version "1.8.20" apply false id 'com.google.gms.google-services' version '4.3.15' apply false } include ":app"
解决方案
1. 解决SDK处理警告
Android Studio与SDK命令行工具版本不匹配导致,操作步骤:
- 打开Android Studio → 菜单栏「Tools」→「SDK Manager」
- 切换到「SDK Tools」标签,勾选右上角「Show Package Details」
- 找到「Android SDK Command-line Tools」,选择与当前Android Studio版本匹配的版本(比如Android Studio Hedgehog对应命令行工具10+,支持XML版本4)
- 点击「Apply」完成更新,重启Android Studio
2. 解决重复导入冲突
Flutter新版本的material包新增了CarouselController类,与第三方库carousel_slider旧版本冲突,两种解决方式:
方式一:升级第三方库(推荐)
打开项目的pubspec.yaml,将carousel_slider版本升级到5.x及以上(新版本已重命名冲突类),然后执行:
flutter pub get
方式二:修改导入别名(临时方案)
如果暂时无法升级,在项目中使用carousel_slider的导入处添加别名:
import 'package:carousel_slider/carousel_slider.dart'; import 'package:carousel_slider/carousel_controller.dart' as sliderCtrl;
使用时通过别名区分:
final controller = sliderCtrl.CarouselController();
辅助操作:清理Pub缓存
执行以下命令清理缓存后重新拉取依赖:
flutter pub cache clean flutter pub get
3. 解决Gradle版本兼容性问题
当前配置中Gradle版本(8.11.1)与Android Gradle Plugin(AGP 8.1.0)不兼容,需统一版本:
方案A:升级AGP适配高版本Gradle
- 修改项目根目录
build.gradle中的AGP版本为8.6.0(支持Gradle 8.10+):classpath 'com.android.tools.build:gradle:8.6.0' - 修改
settings.gradle中的AGP和Kotlin插件版本,保持与根目录一致:plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.0" id "com.android.application" version '8.6.0' apply false id "org.jetbrains.kotlin.android" version "1.9.21" apply false id 'com.google.gms.google-services' version '4.3.15' apply false } - 修改
gradle-wrapper.properties的Gradle版本为你下载的8.11.1:distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
方案B:降级Gradle适配现有AGP
如果不想升级AGP,将Gradle版本降级到8.2(与AGP 8.1.0兼容),修改gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
4. 统一JVM目标版本
修改app/build.gradle中的kotlinOptions,将JVM目标改为17,与Java版本一致:
kotlinOptions { jvmTarget = "17" }
5. 清理并重新构建
完成以上修改后,执行以下命令清理构建缓存并重新构建:
./gradlew clean flutter run
内容的提问来源于stack exchange,提问作者Korhan_34
相关产品推荐
相关产品推荐

