You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Gradle 8.10+AGP 8.6.0后buildSrc插件同步失败求助

问题

升级至Gradle 8.10搭配AGP 8.6.0(Koala Feature Drop)版本后,项目无法正常同步。项目需通过buildSrc目录向GitHub及JitPack发布插件,但重命名buildSrc目录后可正常构建。

报错信息

class com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure cannot be cast to
class com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
(com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure and
 com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @35ab5cf2)

class com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure cannot be cast to
class com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
(com.android.tools.idea.gradle.project.sync.SyncVariantResultFailure and
 com.android.tools.idea.gradle.project.sync.SyncVariantResultSuccess
are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @35ab5cf2)

Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)

Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.

Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.

In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

排查情况

  • 异常指向AGP的com.android.tools.idea.gradle.project.sync模块,堆栈跟踪定位到com.android.tools.idea.gradle.project.sync.VariantDiscovery.discoverVariantsAndSync(VariantDiscovery.kt:141)
  • 尝试过重新下载依赖、停止Gradle守护进程、清理缓存及删除生成目录,均无效
  • 问题根源在buildSrc目录:
    • 将buildSrc中依赖的com.android.tools.build:gradle-api从8.6.0降级至8.5.2后可正常构建
    • 即使仅应用org.gradle.java-gradle-plugin插件也会触发同步失败
  • 插件使用的libs.versions.toml仅在直接打开buildSrc目录时可见,作为嵌套目录时无法识别

需求:避免SyncVariantResultFailure异常,继续在buildSrc目录运行插件代码,或调整buildSrc的声明方式。

解决方案

1. 临时适配:锁定gradle-api版本至8.5.2

在buildSrc的build.gradle.kts中明确指定gradle-api版本为8.5.2,保持与AGP 8.6.0的兼容:

dependencies {
    implementation("com.android.tools.build:gradle-api:8.5.2")
    // 其他依赖...
}

这是当前验证有效的临时方案,可继续使用buildSrc目录发布插件。

2. 替换buildSrc为独立插件模块

Gradle官方推荐用独立子模块替代buildSrc,避免类加载冲突:

  • 在项目根目录创建gradle-plugins目录(名称可自定义)
  • 将buildSrc中的代码迁移至该目录,创建build.gradle.kts并配置:
    plugins {
        `java-gradle-plugin`
        id("com.android.library") version "8.6.0" apply false
        // 其他必要插件...
    }
    
    gradlePlugin {
        plugins {
            create("yourPluginId") {
                id = "com.yourcompany.yourplugin"
                implementationClass = "com.yourcompany.YourPlugin"
            }
        }
    }
    
    dependencies {
        implementation("com.android.tools.build:gradle-api:8.6.0")
        // 其他依赖...
    }
    
  • 在项目根目录的settings.gradle.kts中添加该模块:
    include(":gradle-plugins")
    
  • 根项目的build.gradle.kts中引用插件:
    plugins {
        id("com.yourcompany.yourplugin") apply false
    }
    

这种方式能避免buildSrc带来的类加载隔离问题,同时解决libs.versions.toml无法识别的问题(独立模块可直接读取根目录的版本文件)。

3. 调整IDE配置,强制类加载隔离

在Android Studio中尝试以下操作:

  • 打开File > Settings > Build, Execution, Deployment > Build Tools > Gradle
  • 勾选Use Gradle from 'gradle-wrapper.properties' file
  • 选择Local Gradle distribution并指定Gradle 8.10的本地路径
  • 重启IDE并重新同步项目

4. 等待AGP修复

该异常属于AGP 8.6.0与buildSrc的兼容性bug,可关注官方更新,等待修复版本发布。

内容的提问来源于stack exchange,提问作者Martin Zeitler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:13:15