Android多模块项目的代码覆盖率报告总是显示为0%。
创始人
2024-10-07 15:34:35
0次

一种可能的解决方法是在所有模块的 build.gradle 文件中添加以下配置:

android { ...

buildTypes {
    release {
        ...
        testCoverageEnabled true
    }
    debug {
        ...
        testCoverageEnabled true
    }
}

applicationVariants.all {
    variant ->
        if (variant.buildType.name == 'release') {
            variant.getAssemble().doLast {
                applyCoverageReport(variant)
            }
        } else {
            variant.getExtensions()
                    .getExtraProperties()
                    .set('android.testInstrumentationRunnerArguments',
                            android.testInstrumentationRunnerArguments +
                                    ["coverage" : "true"])

            variant.getAssemble().doLast {
                tasks.create(name: "create${variant.name}CoverageReport",
                        type: JacocoReport) {
                    group = "Reporting"
                    description = "Generate Jacoco coverage reports for ${variant.name} build."
                    reports {
                        xml.enabled true
                        html.enabled true
                    }
                    additionalSourceDirs.from(
                                variant.javaCompileProvider.get().getSource().get().srcDirs)
                    sourceDirectories.from(
                                files([configurations.compile]) +
                                files([project.android.sourceSets.main.java.srcDirs]))
                    classDirectories.from(
                                files([variant.javaCompileProvider.get().getDestinationDir()]) +
                                files([configurations.runtimeClasspath]))
                    executionData.from(
                                fileTree(dir: "$buildDir/outputs/code_coverage/${variant.dirName}",
                                        includes: [
                                                "*${"$"}{variant.name}*",
                                                "*coverage.ec"
                                        ]))
                }
            }
        }
}

...

}

def applyCoverageReport(variant) { def path = "${project.buildDir}/reports/jacoco/${variant.name}/" def file = new File(path) if (!file.exists()) { file.mkdir() } tasks.create(name: "create${variant.name}CoverageReport", type: JacocoReport) { group = "Reporting" description = "Generate Jacoco coverage reports for ${variant.name} build." reports { xml.enabled true html.enabled true } additionalSourceDirs.from( variant.javaCompileProvider.get().getSource().get().srcDirs) sourceDirectories.from( files([configurations.compile]) + files([project.android.sourceSets.main.java.srcDirs])) classDirectories.from( files([variant.javaCompileProvider.get().getDestinationDir()]) + variant.getJavaLibraries().collect { it.jarFile.getAbsoluteFile() } ) executionData.from( fileTree(dir: "$buildDir/outputs/code_coverage/${variant.dirName}", includes: [ "${"$"}{variant.name}", "*coverage.ec

相关内容

热门资讯

我来分享!wpk安卓下载辅助,... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wpk安卓下载辅助解析、 AI 处理...
必知教程!wpk作弊,WPK透... wpk作弊AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包,断网状...
盘点一款!!wepoker怎么... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
必备教程!wpk辅助器是真的吗... 很多人使用云端 AI 时,都担心wpk辅助器是真的吗、内部资料上传带来隐私风险。这款本地运行的 AI...
玩家必看教程!!德普软件,德扑... 您好,玩家必看教程!!德普软件,德扑之星有挂吗确实有挂神器(果然存在有辅助)这款游戏可以开挂的,确实...
今日百科!微扑克辅助,德扑之星... 今日百科!微扑克辅助,德扑之星有挂么辅助插件(原先真的有辅助器)AI 是一款可以让一直输的玩家修复黑...
分辨真假!德普之星透视免费,微... 分辨真假!德普之星透视免费,微扑克有挂吗工具(原生有辅助)AI 是一款可以让一直输的玩家修复黑科技更...
一分钟揭秘!wepoker到底... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
专业讨论!we poker插件... 很多人使用云端 AI 时,都担心we poker插件、内部资料上传带来隐私风险。这款本地运行的 AI...
记者揭秘!!wpk辅助器,WP... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...