Flutter真机运行Gradle依赖解析失败问题求助
解决Flutter真机调试Gradle无法解析FastInfoset依赖的问题
问题原因
从错误日志可见,Gradle解析com.sun.xml.fastinfoset:FastInfoset:1.2.16的POM文件时,错误将依赖项名称拼写为fastinfosat-project(正确应为fastinfoset-project),这是旧版Gradle依赖解析的bug,同时可能伴随Maven仓库访问不稳定的问题。
解决方案
1. 升级Gradle插件版本
将android/build.gradle中的Gradle插件版本从7.1.2升级到稳定兼容的7.4.2,新版本修复了部分依赖解析问题:
buildscript { // ...其他配置 dependencies { classpath 'com.android.tools.build:gradle:7.4.2' // 修改此处版本号 // ...其他依赖 } }
同时修改gradle/wrapper/gradle-wrapper.properties中的Gradle wrapper版本,保持与插件版本兼容:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
2. 添加国内Maven镜像源
在android/build.gradle的buildscript和allprojects的repositories中添加国内镜像,提升依赖下载稳定性:
buildscript { repositories { maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' } google() mavenCentral() } // ...其他配置 } allprojects { repositories { maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' } google() mavenCentral() } }
3. 强制指定FastInfoset依赖版本
在android/build.gradle的buildscript中添加依赖强制解析规则,确保Gradle拉取正确的依赖:
buildscript { // ...其他配置 configurations.all { resolutionStrategy { force 'com.sun.xml.fastinfoset:FastInfoset:1.2.16' } } }
4. 清理缓存并重新构建
执行以下命令清理缓存,重新拉取依赖:
flutter clean cd android && ./gradlew clean build --refresh-dependencies cd .. && flutter pub get flutter run
环境信息
Flutter版本
Flutter 3.10.6 • channel stable • https://github.com/flutter/flutter.git Framework • revision f468f3366c (11 days ago) • 2023-07-12 15:19:05 -0700 Engine • revision cdbeda788a Tools • Dart 3.0.6 • DevTools 2.23.1
项目依赖配置
dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.5 firebase_core: ^2.1.1 cloud_firestore: ^4.0.3 firebase_auth: ^4.1.0 provider: ^6.0.4 flutter_secure_storage: ^8.0.0 dio: ^5.3.0 dartz: ^0.10.1 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.1
错误日志
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'android'. > Could not resolve all files for configuration ':classpath'. > Could not resolve com.sun.xml.fastinfoset:FastInfoset:1.2.16. Required by: project : > com.android.tools.build:gradle:7.1.2 > org.glassfish.jaxb:jaxb-runtime:2.3.2 > Could not resolve com.sun.xml.fastinfoset:FastInfoset:1.2.16. > Could not parse POM https://repo.maven.apache.org/maven2/com/sun/xml/fastinfoset/FastInfoset/1.2.16/FastInfoset-1.2.16.pom > Could not find fastinfosat-project-1.2.16.pom (com.sun.xml.fastinfoset:fastinfosat-project:1.2.16). Searched in the following locations: https://repo.maven.apache.org/maven2/com/sun/xml/fastinfoset/fastinfosat-project/1.2.16/fastinfosat-project-1.2.16.pom
相关Gradle配置文件
settings.gradle
include ':app' def localPropertiesFile = new File(rootProject.projectDir, "local.properties") def properties = new Properties() assert localPropertiesFile.exists() localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } def flutterSdkPath = properties.getProperty("flutter.sdk") assert flutterSdkPath != null, "flutter.sdk not set in local.properties" apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
android/build.gradle
buildscript { ext.kotlin_version = '1.9.0' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.13' } } 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 }
android/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 plugin: 'com.google.gms.google-services' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 33 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 { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.diferenteweb.spotcafe" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration. minSdkVersion 24 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation platform('com.google.firebase:firebase-bom:31.0.2') implementation 'com.google.firebase:firebase-analytics-ktx' }
内容的提问来源于stack exchange,提问作者Luis Monsalve
相关产品推荐
相关产品推荐

