Android项目中如何修改Jacoco输出目录?
修改Android项目中Jacoco覆盖率报告输出目录的方法
要把Jacoco报告目录从/app/build/reports/jacoco/~改成项目根目录下的/build/~,可以通过在模块级build.gradle(通常是app/build.gradle)中配置Jacoco任务的输出路径实现,具体操作如下:
1. 确认Jacoco插件已启用
先确保模块级build.gradle顶部已经应用Jacoco插件:
plugins { id 'com.android.application' // 若为库模块则用 'com.android.library' id 'jacoco' }
2. 自定义报告输出路径
根据项目结构,选择对应配置方式:
单模块项目(仅app模块)
直接修改jacocoTestReport任务的输出位置,指定到根目录的build文件夹:
tasks.withType(Test) { jacoco.includeNoLocationClasses = true } jacocoTestReport { reports { html.outputLocation = file("${rootProject.buildDir}/reports/jacoco/testDebugUnitTestReport") xml.outputLocation = file("${rootProject.buildDir}/reports/jacoco/testDebugUnitTestReport.xml") csv.outputLocation = file("${rootProject.buildDir}/reports/jacoco/testDebugUnitTestReport.csv") } // 关联测试任务,确保执行顺序正确 dependsOn testDebugUnitTest }
${rootProject.buildDir}指向项目根目录的build文件夹,最终报告会生成在/build/reports/jacoco/路径下。- 可根据构建变体修改
testDebugUnitTestReport名称,比如Release变体对应testReleaseUnitTestReport。
多模块项目
如果项目包含多个模块,希望各模块报告统一输出到根目录build下,可在根级build.gradle中统一配置:
subprojects { apply plugin: 'jacoco' tasks.withType(Test) { jacoco.includeNoLocationClasses = true } jacocoTestReport { reports { html.outputLocation = file("${rootProject.buildDir}/reports/jacoco/${project.name}/testDebugUnitTestReport") xml.outputLocation = file("${rootProject.buildDir}/reports/jacoco/${project.name}/testDebugUnitTestReport.xml") csv.outputLocation = file("${rootProject.buildDir}/reports/jacoco/${project.name}/testDebugUnitTestReport.csv") } dependsOn testDebugUnitTest } }
这种配置会让每个模块的报告放在/build/reports/jacoco/[模块名称]/下,避免不同模块的报告冲突。
3. 生成报告
配置完成后,执行对应Gradle任务生成报告:
- Debug变体:
./gradlew jacocoTestDebugUnitTestReport - Release变体:
./gradlew jacocoTestReleaseUnitTestReport
执行完成后,就能在项目根目录的build/reports/jacoco/下找到覆盖率报告。
内容的提问来源于stack exchange,提问作者Denis Jung
相关产品推荐
相关产品推荐

