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

升级com.android.tools.build:gradle至4.2.1后DexProtector报getVariantManager错误

问题根因

Android Gradle Plugin(AGP) 4.2+版本正式移除了废弃已久的getVariantManager()公开API,低于兼容版本的DexProtector插件仍依赖该API实现构建逻辑,因此升级AGP到4.2.1后会触发调用报错,报错信息如下:
DexProtector getVariantManager错误截图

排查解决步骤
  • 方案1:升级DexProtector到兼容版本
    DexProtector从8.2.15版本开始正式适配AGP 4.2.x系列,直接修改项目中DexProtector插件的依赖版本到8.2.15或更高版本即可解决该API调用问题,参考配置:
    // 根目录build.gradle
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        // 升级到支持AGP 4.2的DexProtector版本
        classpath "com.licel:dexprotector-gradle-plugin:8.2.15"
    }
    
  • 方案2:临时降级AGP(无法升级DexProtector时使用)
    若当前项目暂不支持升级DexProtector版本,可将AGP版本回退到4.1.x最新稳定版(如4.1.3),该版本仍保留getVariantManager()接口,可正常兼容旧版本DexProtector。
  • 适配版本后仍报错的排查操作
    若升级到兼容版本后依然触发报错,按以下顺序核对:
    1. 检查DexProtector插件的apply顺序,必须在com.android.application或com.android.library插件之后apply,否则无法正确读取变体配置
    2. 执行构建命令时添加-d参数输出详细调试日志,定位具体报错位置,检查是否存在自定义DexProtector task使用了废弃API的情况
    3. 清理本地构建缓存,执行./gradlew clean build --refresh-dependencies重新拉取依赖后再构建

内容的提问来源于stack exchange,提问作者Pedro Cardozo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 00:36:04