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 s.r.o
Linux 4.14.14-300.fc27.x86_64
I am trying to use the jacoco to generate code coverage. However, when I run the command ./gradlew tasks
I don't see any tasks called jacocoTestReport
.
I get the below error when I try and run the 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 have tried to clean and rebuild the project. However, the reporting task isn't there.
Many thanks for any suggestions.
Hi, you are actually configuring the test task during execution ( doLast happens after the task's actions executed). You should be configuring the test task in the configuration phase (without the doLast ).
JaCoCo Report configurationThe JacocoReport task can be used to generate code coverage reports in different formats. It implements the standard Gradle type Reporting and exposes a report container of type JacocoReportsContainer. Example 4. Configuring test task. build.gradle.
You are searching for a wrong task to execute. By performing ./gradlew tasks
you'll be able to find createFlavorCoverageReport tasks:
After executing ./gradlew createDevDebugCoverageReport
with the setup that you have mentioned in the question I was able to find generated report at /app/build/reports/dev/debug
directory.
There are several things which we have to take care while using jacoco report which as follows:
Enabled test coverage in app/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
Generated jacoco report path after successful jacocoTestReport execution.
app/build/reports/coverage/debug/index.html
Also, I have created one android jacoco related sample repository where you can look.
https://github.com/jiteshmohite/JacocoAndroidSample
Also, please ensure you are running Gradle command inside the application directory.
Try the above-mentioned sample repository for reference. I created this with zero complexities, So everyone can go and use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With