Skip to content

Execution failed for task ':app:mapDebugSourceSetPaths' 错误解决指南

注意

本文主要针对 Android 开发中遇到的 Execution failed for task ':app:mapDebugSourceSetPaths' 错误,特别是与 extraGeneratedResDir 属性和 Google 服务插件相关的版本兼容性问题。

问题描述

当您在使用 Android Studio 开发应用时(特别是使用了 Firebase 或其他 Google 服务的项目),可能会遇到以下构建错误:

Execution failed for task ':app:mapDebugSourceSetPaths'
> Error while evaluating property 'extraGeneratedResDir' of task ':app:mapDebugSourceSetPaths'
Failed to calculate the value of task ':app:mapDebugSourceSetPaths' property 'extraGeneratedResDir'
> Querying the mapped value of provider(java.util.Set) before task ':app:processDebugGoogleServices' has completed is not supported

这个问题通常表现为一个"死循环":移除 com.google.gms.google-services 插件可以完成构建,但会导致 Firebase 功能失效;而添加该插件又会导致构建失败。

根本原因

此问题的核心原因是版本兼容性冲突。具体来说:

  • Android Gradle 插件版本 (AGP) 与 Google 服务插件版本不兼容
  • 新旧版本插件之间的 API 变更导致的构建顺序问题
  • Gradle 配置中对资源目录的评估时机不正确

解决方案

方案一:更新 Google 服务插件版本(推荐)

这是最常见且有效的解决方案。您需要确保项目中使用兼容的版本组合。

  1. 打开项目根目录的 build.gradle 文件
  2. dependencies 块中更新 Google 服务插件版本
gradle
buildscript {
    dependencies {
        // 使用兼容的版本组合
        classpath 'com.android.tools.build:gradle:8.3.2'        // AGP 版本
        classpath 'com.google.gms:google-services:4.4.1'        // Google 服务版本
    }
}

最新版本查询

建议访问 Google 服务插件官方文档 获取最新的兼容版本信息。

方案二:完整配置示例

对于不同类型的项目,以下是推荐的配置方法:

gradle
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
gradle
plugins {
    id 'com.android.application' version '8.3.2' apply false
    id 'com.android.library' version '8.3.2' apply false
    id 'com.google.gms.google-services' version '4.4.1' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
gradle
plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    // 其他插件...
}
gradle
buildscript {
    ext.kotlin_version = '1.7.10'
    
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.3.2'
        classpath 'com.google.gms:google-services:4.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
gradle
<!-- 在 config.xml 中添加 -->
<preference name="GradlePluginGoogleServicesEnabled" value="true" />
<preference name="GradlePluginGoogleServicesVersion" value="4.4.1" />

方案三:清理和重建项目

更新版本后,执行以下清理操作:

bash
# Flutter 项目
flutter clean
flutter pub get

# Android 项目
./gradlew clean
./gradlew build

兼容性参考表

以下是一些经过验证的兼容版本组合:

Android Studio 版本AGP 版本Google 服务版本状态
Iguana 2023.2.18.3.24.4.1✅ 推荐
Electric Giraffe 2022.3.18.1.34.4.0✅ 可用
Electric Eel7.4.04.3.15✅ 可用
Dolphin7.3.14.3.14✅ 可用

注意事项

  1. 不要随意删除插件:虽然删除 com.google.gms.google-services 插件可以暂时解决构建问题,但这会破坏 Firebase 功能。

  2. 检查其他依赖:如果您使用了其他 Firebase 服务(如 Crashlytics、Performance Monitoring等),也需要确保它们的版本与 Google 服务插件兼容。

  3. Gradle 版本:确保 Gradle 包装器版本与 AGP 版本兼容。通常 Android Studio 会提示推荐的 Gradle 版本。

  4. 缓存问题:如果问题持续存在,尝试清理 Android Studio 缓存(File > Invalidate Caches / Restart)。

结论

Execution failed for task ':app:mapDebugSourceSetPaths' 错误通常由版本不兼容引起,通过更新 Google 服务插件到与 Android Gradle 插件兼容的版本即可解决。建议定期检查并更新到官方推荐的最新兼容版本,以保持项目的稳定性和安全性。

提示

如果问题仍然存在,请检查项目的其他配置,如 compileSdkVersionminSdkVersiontargetSdkVersion 的设置,确保它们与插件版本兼容。