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

升级Android Gradle Plugin 8.4.1后mergeDebugAndroidTestJavaResource构建失败求助

解决AGP 8.4.1中JUnit依赖重复META-INF/LICENSE.md的构建错误

1. 精准配置测试变体的Packaging Options

AGP 8.x对变体资源合并的控制更严格,之前的全局配置可能未覆盖测试变体,需针对性调整:

android {
    // 全局覆盖所有变体(含测试变体)的资源规则
    packaging {
        resources {
            // 优先尝试排除重复文件
            excludes += "META-INF/LICENSE.md"
            // 若排除无效,强制保留第一个出现的文件
            pickFirsts += "META-INF/LICENSE.md"
        }
    }

    // 或针对测试变体单独配置
    buildTypes {
        debug {
            testResources {
                packaging {
                    resources {
                        excludes += "META-INF/LICENSE.md"
                    }
                }
            }
        }
    }
}

2. 统一JUnit依赖版本并排查冲突

依赖版本不一致是重复文件的常见诱因,先强制统一JUnit相关依赖版本:

allprojects {
    configurations.all {
        resolutionStrategy {
            force 'junit:junit:4.13.2'
            force 'org.junit.jupiter:junit-jupiter-api:5.10.0'
            force 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
            // 根据项目实际使用的JUnit依赖补充
        }
    }
}

再执行./gradlew :core-test:dependencies生成依赖树,定位重复文件的来源依赖,手动排除冗余模块:

dependencies {
    androidTestImplementation('org.junit.jupiter:junit-jupiter-api:5.10.0') {
        exclude group: 'org.junit.platform', module: 'junit-platform-commons'
        // 依据依赖树中的重复项调整排除规则
    }
}

3. 启用调试日志定位重复源

在gradle.properties中添加以下配置,获取更详细的资源合并日志,精准定位重复文件的来源:

android.mergeJavaResourcesLoggingLevel=DEBUG
android.debug.obsoleteApi=true

重新构建后,在日志中搜索META-INF/LICENSE.md,获取所有产生重复的依赖包信息,针对性处理。

4. 清理模块级缓存并重构

除全局.gradle缓存外,清理模块本地构建缓存:

  • 执行命令:./gradlew :core-test:clean
  • 删除core-test/build目录
  • 重新执行构建:./gradlew :core-test:assembleDebugAndroidTest

内容的提问来源于stack exchange,提问作者Khushboo Gandhi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 04:05:56