how to prevent jacoco instrumenting production code?

隐身守侯 提交于 2019-11-28 11:02:19

问题


i use jacoco plugin for gradle:

apply plugin: 'kotlin'

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

and then i want to build a package for production

./gradlew build jacocoTestReport

the question is: will the generated package be instrumented by jacoco? if yes, how can build package NOT instrumented = ready for production? and having code coverage run? do i have to run build twice? is it impossible to build code once (sign it) and then test it, measure coverage etc and if all checks passes, deploy it?


回答1:


JaCoCo provides two ways of performing instrumentation:

  • so-called "on-the-fly" using Java Agent - http://www.jacoco.org/jacoco/trunk/doc/agent.html
  • and so-called "offline" - http://www.jacoco.org/jacoco/trunk/doc/offline.html

The difference is that in first case instrumentation happens in memory during execution and so no class or jar files will be changed on disk - quoting 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.

So one of the simplifications that Java agent brings - is exactly that you don't need to worry about packaging or multiple builds. This is IMO one of the advantages of JaCoCo over other coverage tools for Java such as Cobertura and Clover.

And this is one of the reasons why it is highly recommended to use on-the-fly instrumentation - quoting 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.

One of such specific scenarios - is execution of tests on Android, because there is no way to use Java agent on it. So AFAIK Android Plugin for Gradle, when instructed to measure coverage using JaCoCo, uses offline instrumentation and therefore requires two types of build - with coverage and without for release.

On the other hand JaCoCo Gradle Plugin, which integrates JaCoCo into Gradle for Java projects, AFAIK as of today provides ability to perform only on-the-fly instrumentation and not offline.



来源:https://stackoverflow.com/questions/49296416/how-to-prevent-jacoco-instrumenting-production-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!