如何防止 jacoco 检测生产代码?

how to prevent jacoco instrumenting production code?

我使用 jacoco 插件 gradle:

apply plugin: 'kotlin'

jacoco {
    toolVersion = "0.7.9"
}
jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled false
        csv.enabled false
    }
}

然后我想构建一个用于生产的包

./gradlew build jacocoTestReport

问题是:生成的包会被 jacoco 检测吗?如果是,如何构建未检测的包 = 准备好生产?代码覆盖率 运行?我必须 运行 构建两次吗?是否不可能一次构建代码(对其签名)然后对其进行测试、测量覆盖率等,如果所有检查都通过,则部署它?

JaCoCo 提供了两种执行检测的方法:

不同之处在于,在第一种情况下,检测在执行期间发生在内存中,因此磁盘上不会更改 class 或 jar 文件 - 引用 the second link:

One of the main benefits of JaCoCo is the Java agent, which instruments classes on-the-fly. This simplifies code coverage analysis a lot as no pre-instrumentation and classpath tweaking is required.

因此,Java 代理带来的简化之一就是您无需担心打包或多次构建。这是 IMO JaCoCo 优于 other coverage tools for Java 如 Cobertura 和 Clover 的优势之一。

这也是强烈推荐使用 on-the-fly 检测的原因之一 - 引用 http://www.jacoco.org/jacoco/trunk/doc/cli.html :

the preferred way for code coverage analysis with JaCoCo is on-the-fly instrumentation with the JaCoCo agent. Offline instrumentation has several drawbacks and should only be used if a specific scenario explicitly requires this mode.

其中一个特定场景 - 是在 Android 上执行测试,因为无法在其上使用 Java 代理。因此 AFAIK Android Plugin for Gradle,当指示使用 JaCoCo 测量覆盖率时,使用离线检测,因此需要两种类型的构建 - 有覆盖率和没有发布。

另一方面,JaCoCo Gradle Plugin, which integrates JaCoCo into Gradle for Java projects, AFAIK 目前仅提供 on-the-fly 检测而非离线的能力。