JacocoTestReport task not created

Android Studio 3.1 Canary 8 Build #AI-173.4529993, built on January 6, 2018 JRE: 1.8.0_152-release-1024-b01 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains sro Linux 4.14.14-300.fc27.x86_64 

I am trying to use jacoco to create code coverage. However, when I run the ./gradlew tasks command, I do not see tasks called jacocoTestReport .

I get the following error when trying to run tasks ./gradlew jacocoTestReport :

Task 'jacocoTestReport' not found in root project 'EnumSample'

This is my build.gradlew file:

 apply plugin: 'com.android.application' apply plugin: 'jacoco' android { compileSdkVersion 27 defaultConfig { applicationId "me.androidbox.enumsample" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } debug { testCoverageEnabled true } } } jacoco { toolVersion "0.8.0" } task jacocoTestReport(type: JacocoReport) { executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec") subprojects.each { sourceSets it.sourceSets.main } reports { xml.enabled true html.enabled false csv.enabled false } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.0.2' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' } 

I tried to clean and rebuild the project. However, reporting tasks do not exist.

Thanks so much for any suggestions.

+8
android unit-testing code-coverage gradle jacoco
source share
3 answers

You are doing the wrong task. By performing ./gradlew tasks , you can find createFlavorCoverageReport tasks:

enter image description here

After doing ./gradlew createDevDebugCoverageReport with the installation that you mentioned in the question, I managed to find the generated report in the /app/build/reports/dev/debug .

+3
source share

There are a few things we should take care of when using the jacoco report, which looks like this:

Enabled test coverage in /build.gradle

 android { ... buildTypes { debug { testCoverageEnabled true } ... } } 

Create task for jacoco report

 apply plugin: 'jacoco' task jacocoTestReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') { reports { xml.enabled = true html.enabled = true } def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*'] def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter) def mainSrc = "${project.projectDir}/src/main/java" sourceDirectories = files([mainSrc]) classDirectories = files([debugTree]) executionData = files("${buildDir}/jacoco/testDebugUnitTest.exec") } 

Gradle command for jacoco report

./gradlew clean jacocoTestReport

Find jacoco report here

Created jacoco report path after jacocoTestReport successfully completed.

application / build / reports / covers / debugs / index.html

In addition, I created one of the jacoco related repositories that you can watch.

https://github.com/jiteshmohite/JacocoAndroidSample

Also, make sure you use the Gradle command inside the application directory.

Try the above sample repository for reference. I created this with zero complexity, so anyone can go and use it.

+3
source share

There are two things:

  • You need to enable code support for the type of assembly with which you will test. Your build.gradle should include the following (which you already included):

     android { ... buildTypes { debug { testCoverageEnabled = true } ... } ... } 
    • To create a report, run gradle testBlueDebugUnitTestCoverage and you will see them in "assembly / reports / jacoco / testBlueDebugUnitTestCoverage /"
    • Here is a gitHub example

  • Use the Gradle plugin that generates JaCoCo reports:

    Install it like this:

     buildscript { repositories { mavenCentral() } dependencies { classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.11.0' } } apply plugin: 'com.vanniktech.android.junit.jacoco' 

Another release solution reported here :

 task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") { group = "Reporting" description = "Generate Jacoco coverage reports after running tests." reports { xml.enabled = false html.enabled = true } classDirectories = fileTree( dir: './build/classes/debug', excludes: ['**/R.class', '**/R$*.class', '**/*$InjectAdapter.class', '**/*$ModuleAdapter.class', '**/*$ViewInjector*.class' ]) sourceDirectories = files(coverageSourceDirs) executionData = files('build/jacoco/testDebug.exec') renamedFilesMap = [:] // Hacky fix for issue: https://code.google.com/p/android/issues/detail?id=69174. // Rename files with '$$' before generating report, and then rename back after doFirst { new File('build/classes/debug').eachFileRecurse { file -> if (file.name.contains('$$')) { oldPath = file.path newPath = oldPath.replace('$$', '$') file.renameTo(newPath) renamedFilesMap[newPath] = oldPath } } } doLast { renamedFilesMap.each() { newPath, oldPath -> new File(newPath).renameTo(oldPath) } } } 
+2
source share

All Articles