build.gradle
should contain the following:
android { ... buildTypes { debug { testCoverageEnabled = true } ... } ... }
build.gradle
, such a “mounted installation” will make your build script unreadable, so I recommend putting all this into a separate build script, and then importing.
jacoco.gradle
file in the project root directory. You can create it anywhere you want, but keeping it in the project root directory will make it easy to refer to it from all subprojects.
apply plugin: 'jacoco' jacoco { toolVersion = "0.7.5.201505241946" }
def buildTypes = android.buildTypes.collect { type -> type.name } def productFlavors = android.productFlavors.collect { flavor -> flavor.name }
collect
in Groovy takes a list as input, calls the function with each item in the list, and returns the results in a new list. In this case, the input receives lists of objects “assembly mode” and “product variation”, which are converted into lists of their names.
if (!productFlavors) productFlavors.add('')
productFlavors.each { productFlavorName -> buildTypes.each { buildTypeName -> ... } }
sourceName
- the name of the build source (build source name), for blueDebug
: blueDebug
sourcePath
- the path to the source codes of the build (build source path), eg: blue/debug
testTaskName
is the task for executing tests on which the task of measuring code coverage will depend, for testBlueDebug
: testBlueDebug
def sourceName, sourcePath if (!productFlavorName) { sourceName = sourcePath = "${buildTypeName}" } else { sourceName = "${productFlavorName}${buildTypeName.capitalize()}" sourcePath = "${productFlavorName}/${buildTypeName}" } def testTaskName = "test${sourceName.capitalize()}UnitTest"
task "${testTaskName}Coverage" (type:JacocoReport, dependsOn: "$testTaskName") { group = "Reporting" description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build." classDirectories = fileTree( dir: "${project.buildDir}/intermediates/classes/${sourcePath}", excludes: ['**/R.class', '**/R$*.class', '**/*$ViewInjector*.*', '**/BuildConfig.*', '**/Manifest*.*'] ) def coverageSourceDirs = [ "src/main/java", "src/$productFlavorName/java", "src/$buildTypeName/java" ] additionalSourceDirs = files(coverageSourceDirs) sourceDirectories = files(coverageSourceDirs) executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec") reports { xml.enabled = true html.enabled = true } }
classDirectories
- in "excludes"
you can list templates for exclusion from the report; it may be generated code (class R
, code embedding dependencies, etc.) or anything else you want to ignorereports
- allows HTML and / or XML reports, depending on whether they are needed for publication or for analysis, respectively.jacoco.gradle
, so here is the full contents of the file:
apply plugin: 'jacoco' jacoco { toolVersion = "0.7.5.201505241946" } project.afterEvaluate { // Grab all build types and product flavors def buildTypes = android.buildTypes.collect { type -> type.name } def productFlavors = android.productFlavors.collect { flavor -> flavor.name } // When no product flavors defined, use empty if (!productFlavors) productFlavors.add('') productFlavors.each { productFlavorName -> buildTypes.each { buildTypeName -> def sourceName, sourcePath if (!productFlavorName) { sourceName = sourcePath = "${buildTypeName}" } else { sourceName = "${productFlavorName}${buildTypeName.capitalize()}" sourcePath = "${productFlavorName}/${buildTypeName}" } def testTaskName = "test${sourceName.capitalize()}UnitTest" // Create coverage task of form 'testFlavorTypeCoverage' depending on 'testFlavorTypeUnitTest' task "${testTaskName}Coverage" (type:JacocoReport, dependsOn: "$testTaskName") { group = "Reporting" description = "Generate Jacoco coverage reports on the ${sourceName.capitalize()} build." classDirectories = fileTree( dir: "${project.buildDir}/intermediates/classes/${sourcePath}", excludes: ['**/R.class', '**/R$*.class', '**/*$ViewInjector*.*', '**/*$ViewBinder*.*', '**/BuildConfig.*', '**/Manifest*.*'] ) def coverageSourceDirs = [ "src/main/java", "src/$productFlavorName/java", "src/$buildTypeName/java" ] additionalSourceDirs = files(coverageSourceDirs) sourceDirectories = files(coverageSourceDirs) executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec") reports { xml.enabled = true html.enabled = true } } } } }
app
something like this:
apply from: '../jacoco.gradle'
jacoco.gradle
is located in the root directory of your project, as described above.)
gradle tasks
and looking for something like the following in the "Reporting"
section:
Reporting tasks --------------- testBlueDebugUnitTestCoverage - Generate Jacoco coverage reports on the BlueDebug build. testBlueReleaseUnitTestCoverage - Generate Jacoco coverage reports on the BlueRelease build. testRedDebugUnitTestCoverage - Generate Jacoco coverage reports on the RedDebug build. testRedReleaseUnitTestCoverage - Generate Jacoco coverage reports on the RedRelease build.
gradle testBlueDebugUnitTestCoverage
and you will find it in "build/reports/jacoco/testBlueDebugUnitTestCoverage/"
.
Source: https://habr.com/ru/post/280374/