Why does android Instrumentation test report fail when coverage is enabled? - android

I am trying to generate jacoco reports, but I keep getting an error when I set the testCoverageEnabled property to true.
java.lang.NoSuchMethodError: No static method $jacocoInit()[Z in class L...
Removing the testCoverageEnabled property I can run the tests but I can not generate reports.
I am using standard Jacoco configurations. Unit tests and integration tests run properly when run independently.
All online references to this issue seem to be specific configuration issues within projects. Any help is appreciated.
Update
apply plugin: 'jacoco'
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
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 = fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
])
}

Related

How to generate a Jacoco coverage report for android connected tests with Jacoco version 0.8.3+?

I am attempting to generate instrumentation test coverage reports in Gradle to send to SonarQube. As this is a Kotlin codebase, I need to use Jacoco version 0.8.3+ to generate the reports to avoid the issue outlined here.
The custom task I created for generating unit test coverage reports works fine with the specified Jacoco version. But instrumented test coverage reports generated with the ./gradlew createDebugCoverageReport still generates a report with Jacoco version 0.7.9.
Is there a way to make ./gradlew createDebugCoverageReport or some other way to create an instrumentation test coverage report with a newer version of Jacoco plugin 0.8.3+?
Relevant parts of the top level build.gradle file:
buildscript {
dependencies {
classpath "org.jacoco:org.jacoco.core:0.8.5"
}
}
Relevant parts of the module level build.gradle file:
apply plugin:"com.android.library"
apply plugin: "jacoco"
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
}
jacoco {
toolVersion = "0.8.5"
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) {
group "Reporting"
description "Generate Jacoco coverage reports from unit tests."
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter =
['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*',
'android/**/*.*']
def javaDebugTree =
fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def kotlinDebugTree =
fileTree(dir: "${buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories.from.addAll(files([mainSrc]))
classDirectories.from.addAll(files([javaDebugTree, kotlinDebugTree]))
executionData.from.addAll(fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
]))
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
Running the tasks sequentially test connectedDebugAndroidTest jacocoTestReport generates two sets of jacoco reports. The report from the connected tests with the older jacoco version, and the report for the unit tests with the correct jacoco version.

How to get code coverage for unit and robelectric code coverage using Jacoco plugin?

I have been using the Jacoco plugin to get code coverage for my android project. Currently on executing the test using this command,
gradlew test jacocoTestReport
It runs only the robolectric and UI test. I want to run the Unit test and the robolectric test instead.
Following is the task for Jacoco created inside build.gradle,
task jacocoTestReport(type: JacocoReport, dependsOn: ['test', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*' ]
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/test.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
}
Is there any way we can achieve the code coverage for unit and robolectric test using Jacoco. Please help if you have any idea regarding this

How to run jacocoTestReport when including Android instrumental tests?

My Android app contains both Unit and Instrumental tests, and I'd like to generate a coverage report using the JaCoCo plugin (and then finally porting to coveralls.io ) . But I cannot seem to run gradlew jacocoTestReport on my local machine, because I don't have a emulator or device that connects to Android Studio. The error and complete log is up at https://pastebin.com/c7vcwseF .
Here's the portion of my app's build.gradle that contains the task definition:
# ...
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$project.buildDir/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: project.buildDir, includes: [
'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
])
}
coveralls {
jacocoReportPath 'build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml'
}
# ...
I am using the org.jacoco:org.jacoco.core:0.8.2 plugin in my Android project.
So how would one run gradlew jacocoTestReport and generate the report locally?

How to exclude Fragment from Jacoco code coverage

I have used below code to exclude fragment but I unable to exlcude fragment from jacoco code coverage kindly help me on this.
task jacocoTestReport(type: JacocoReport) {
def coverageSourceDirs = [
'src/main/java'
]
group = "Reporting"
description = "Generates Jacoco coverage reports"
reports {
xml {
enabled = true
destination "${buildDir}/reports/jacoco/jacoco.xml"
}
csv.enabled false
html {
enabled true
destination "${buildDir}/jacocoHtml"
}
}
classDirectories = fileTree(
dir: 'build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Fragment*.*'
]
)
sourceDirectories = files(coverageSourceDirs)
additionalSourceDirs = files(coverageSourceDirs)
executionData = files('build/outputs/code - coverage/connected/flavors/smartcompanion/coverage.ec')
}
We used command below :-
gradlew connectedCheck
gradlew connectedAndroidTest
gradlew connectedDubugAndroidTest
Everytime in coverage report we able to see fragment.
There is another way to exclude files, you can filter them out:
classDirectories = fileTree(
dir: 'build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Fragment*.*'
]
).filter ({file -> !file.name.contains('Fragment')})
For some reason I was also unable to exclude fragments. I couldn't find out what went wrong. Filtering them out by name worked though. So it kinda feels like a workaround, but at least I could move on and get my work done.
I also noticed that on Android, the different versions of Gradle and the jacoco plugin are not always very compatible. So it might help to experiment with downgrading (or upgrading) too.

Jacoco code coverage details missing on Jenkins

I am trying to use the code coverage tools of Jacoco on a Jenkins server for my Android library.
The builds are successful, and the code coverage stats are shown in the report :
However, when I want the details of which lines are covered or not, the page is totally empty:
As this is the thing I need the most, this is pretty annoying...
Here is the part of the gradle file I have for Jacoco:
apply plugin: 'jacoco'
jacoco {
// Use this version for upper ones are broken (with Gradle)
// https://github.com/jacoco/jacoco/issues/288
toolVersion = "0.7.1.201405082137"
}
// Edit covered scope if needed
def coverageSourceDirs = [
'../s3papiandroidclient/src'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "test") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../s3papiandroidclient/build/intermediates/classes',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testUnitTestDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
jacocoTestReport.doFirst{
delete fileTree (dir: "../s3papiandroidclient/build/intermediates/classes", include: "**/*Test.class")
}
Anyone encountered something similar ?
Regards,
Gyoo.

Categories

Resources