I'm using Robolectric and JaCoCo together. My code coverage reports do not work without the following lines of code in gradle script:
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
But in the recent version of Gradle the JaCoCo extension, that I use here, is marked as deprecated. I could not find any replacement for it. So, where should I apply the includeNoLocationClasses = true option?
Using the Gradle Kotlin DSL with Gradle 5.5.1 and Kotlin 1.3.31 this works:
tasks {
withType<Test> {
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
}
}
}
I found a solution. JaCoCo automatically adds jacoco extension to all tasks of test type. So, all that I had to do was adding the following lines into build script:
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
It doesn't look like an official solution, but it allows the custom JacocoReport implementation to work correctly.
Related
I have a kotlin multiplatofrm library that is included into an Android and iOS app.
In my android project include it as a composite build (MyLib). But Intellisense is not working at all for all code from in MyLib, though the whole thing compiles fine. I am using Android Studio. What could be wrong and how can I debug it?
rootProject.name='xxx'
includeBuild 'MyLib'
include ':common'
include ':app'
MyLib's build.gradle.kts looks as follows:
plugins {
kotlin("multiplatform") version "1.5.31"
kotlin("native.cocoapods") version "1.5.31"
}
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/kotlin/kotlinx.html/") }
}
group = "com.xxx.MyLib"
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
ios()
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
cocoapods {
ios.deploymentTarget = "11.4"
frameworkName = "MyLib"
summary = "xxx"
homepage = "xxx"
podfile = project.file("../../iOS-App/Podfile")
}
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
implementation("com.badoo.reaktive:reaktive:1.2.0")
implementation("com.badoo.reaktive:reaktive-annotations:1.2.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
implementation("com.russhwolf:multiplatform-settings-no-arg:0.8.1")
implementation("net.swiftzer.semver:semver:1.1.1")
}
}
}
}
tasks.withType<GenerateModuleMetadata> {
enabled = true
}
I think this is likely related to https://youtrack.jetbrains.com/issue/KTIJ-18903
I had the same issue and it drove me crazy. How can one write code nowadays without Intellisense (answer: you can't).
I tried a ton of things (all the usual and unusual stuff you do when Android Studio / IntelliJ act up). Ultimately I upgraded to Kotlin 1.6.0-RC2 (from 1.5.31) -> https://github.com/JetBrains/kotlin/releases/tag/v1.6.0-RC2.
Part of that is upgrading the Kotlin Plugin:
Another part is the Kotlin Gradle Plugin dependency:
org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0-RC2
And last but not least I had to downgrade the corouting dependency (from 1.5.2):
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-RC
After that everything was back to normal.
I have been looking for a solution for days for merging multiple Jacoco reports for a multi-module Android project in order to send them off to Sonarcloud at once. I have already checked tons of Stackoverflow posts and other things such as blogs, Github repositories, Gradle forum, etc but unfortunately none of solutions works for me.
I would really appreciate if anybody here shares a sample project or code piece with me.
Gradle version: 7.0.2
Kotlin version: 1.5.21
JDK: 11
Below code piece also doesn't work for me
/**
* Root task that generates an aggregated Jacoco test coverage report for all sub-projects
*/
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
group = 'Reporting'
description = 'Generates an aggregate report from all subprojects'
tasks.withType(Test) {
ignoreFailures true
}
def projects = subprojects
//noinspection GrUnresolvedAccess
dependsOn(projects.jacocoReport)
final source = files(projects.jacocoReport.sourceDirectories)
additionalSourceDirs.setFrom source
sourceDirectories.setFrom source
classDirectories.setFrom files(projects.jacocoReport.classDirectories)
executionData.setFrom files(projects.jacocoReport.executionData)
reports {
html {
enabled true
destination file('build/reports/jacoco/html')
}
csv {
enabled true
destination file('build/reports/jacoco/jacocoFullReport.csv')
}
}
doFirst {
//noinspection GroovyAssignabilityCheck
executionData.setFrom files(executionData.findAll { it.exists() })
}
}
Here working example how to create aggregated jacoco report:
https://github.com/SurpSG/jacoco-gradle-plugin-merge-coverage
task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
dependsOn(subprojects.test)
additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(subprojects.sourceSets.main.output)
executionData.from = files(subprojects.jacocoTestReport.executionData)
reports {
html.enabled true
}
}
I just created this demo project illustrating how to archive this with a buildSrc ad-hoc plugin, that relies on Gradle's jacoco-report-aggregation and Android Gradle Plugin's native jacoco support.
Later you can easily link it with sonarqube by adding this to the root project:
plugins {
coverage // comes from `buildSrc/src/main/kotlin/coverage.gradle.kts`
id("org.sonarqube") version "3.4.0.2513"
}
sonarqube {
properties {
tasks.jacocoTestReport.configure {
property("sonar.coverage.jacoco.xmlReportPaths", reports.xml.outputLocation.get().asFile)
}
}
}
tasks.sonarqube.configure {
dependsOn(tasks.jacocoTestReport)
}
I am trying to update an Android project to use the latest gradle plugin (7.0.1), from the current 3.6.4 that it is using. In order to do this, considering the project is using protobuf, I need to update the protobuf and gRPC dependencies, as the current ones are not compatible with the latest plugin.
I have followed https://github.com/grpc/grpc-java in order to use the latest dependency versions. I updated the dependencies to the following versions:
implementation 'io.grpc:grpc-okhttp:1.40.1'
implementation 'io.grpc:grpc-protobuf-lite:1.40.1'
implementation 'io.grpc:grpc-stub:1.40.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
protobuf "com.google.protobuf:protobuf-java:3.17.3"
I am using the latest protobuf plugin
plugins {
id 'com.google.protobuf' version '0.8.17'
}
And use the following block for code-gen
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.17.3"
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.40.1"
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
java { option 'lite' }
}
task.plugins {
grpc { option 'lite' }
}
}
}
}
The gradle sync succeeds while using those, the problem is when I try to assemble the project, I get the following error:
Execution failed for task ':App:generateDebugProto'.
protoc: stdout: . stderr: C:\Users\phantom\AndroidStudioProjects\Protobuf\App\build\extracted-protos\main\google\protobuf\any.proto: Input is shadowed in the --proto_path by "C:/Users/phantom/AndroidStudioProjects/Protobuf/App/build/extracted-include-protos/debug/google/protobuf/any.proto". Either use the latter file as your input or reorder the --proto_path so that the former file's location comes first.
From what I understand while reading the error, the problem is that the proto files are generated now in both extracted-protos and extracted-include-protos build files, and the latter shadows the first one. I have checked, in the previous version, the files were generated solely in the extracted-protos build files.
Is there a way to skip generating the files in the extracted-include-protos? Or what would be the course of action to be able to assemble the project?
I ran into this same issue yesterday. This is more of a workaround than a full answer. It got me working with Google speech-to-text, but it doesn't work if you add in a non-beta version of text-to-speech, so if anybody has a better answer please post.
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.17.3'
}
plugins {
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.40.1"
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
// In most cases you don't need the full Java output
// if you use the lite output.
remove java
}
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
}
implementation 'io.grpc:grpc-okhttp:1.40.1'
implementation 'io.grpc:grpc-protobuf-lite:1.25.0'
implementation 'io.grpc:grpc-stub:1.40.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53'
protobuf "com.google.protobuf:protobuf-java:3.17.3"
implementation("com.google.cloud:google-cloud-speech:1.22.1") {
exclude group: 'com.google.protobuf', module: 'protobuf-java'
exclude group: 'com.google.api.grpc'
}
Note the versions of grpc-protobuf-lite and google-cloud-speech. I had to downgrade both of them from the latest.
Had the same issue,
After trying every possible gradle configuration for 2 days.
I just manually deleted all the proto files under ../build/extracted-include-protos/debug/google/protobuf/
and the error is gone
Today I updated gradle and kotlin dependencies in android studio.
The new versions are these:
kotlin_version = "1.5.10"
...
jacoco {
toolVersion = "0.8.6"
}
...
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
The test coverage report task fails with the following error:
2021-05-27T16:57:49.150+0200 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter] Executing actions for task ':consumerkit:testDebugUnitTestCoverage'.
2021-05-27T16:57:49.304+0200 [DEBUG] [org.codehaus.groovy.vmplugin.VMPluginFactory] Trying to create VM plugin `org.codehaus.groovy.vmplugin.v9.Java9` by checking `java.lang.Module`, but failed:
java.lang.ClassNotFoundException: java.lang.Module
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.codehaus.groovy.vmplugin.VMPluginFactory.lambda$createPlugin$0(VMPluginFactory.java:61)
at java.security.AccessController.doPrivileged(Native Method)
For Kotlin 1.5 you should use JaCoCo 0.8.7 instead of 0.8.6 - see https://github.com/jacoco/jacoco/pull/1164 and the full changelog at https://www.jacoco.org/jacoco/trunk/doc/changes.html
Example snippet:
// build.gradle or build.gradle.kts
jacoco {
toolVersion = "0.8.7"
}
I already had the
jacoco {
toolVersion = "0.8.7"
}
configured but it was not working anyway, what fixed the problem was to follow this comment here
In your module build.gradle switch back to:
android {
//....
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
don't worry, it won't break anything if you were not using Java 11 language features yet, AGP 7 is still compatible with JDK 8 as target.
Then you need to force AGP to use the 0.8.7 version and not the default 0.8.3 one. In your allprojects block in the root build.gradle file, add this:
allprojects {
//... other things
// workaround to fix an auto-import of a lower Jacoco version
resolutionStrategy {
eachDependency { details ->
if ('org.jacoco' == details.requested.group) {
details.useVersion "0.8.7"
}
}
}
}
and now it should work using:
AGP 7.0.X
Kotlin 1.5.X
JDK 11 (embedded with AS)
Simply doing
jacoco {
toolVersion = "0.8.7"
}
was not enough for me. I also had to override the version that Android was using so that the androidJacocoAnt dependency also uses 0.8.7. (./gradlew app:dependencies) Simply add this to your gradle too
android.jacoco.version = "0.8.7"
It's quite old thread, but these solutions did not solve my problems completely in Android project (actually written in Java - not in Kotlin), so I will add my solutions here. Maybe someone will find it helpful.
Except of updating jacoco toolVersion to 0.8.7, I also had to update execution data in my jacoco config as follows:
project.afterEvaluate {
tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport,
dependsOn: [
"$unitTestTask",
":sdk:testDebugUnitTest",
":sdk:connectedCheck"
]) {
/* all jacoco custom configuration goes here... */
executionData(fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec",
"outputs/code_coverage/debugAndroidTest/connected/*coverage.ec"
]))
}
}
in this configuration I had to add the following line:
"outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec"
which wasn't there before because file with generated report used by sonar was generated in the new location. Report is generated with gradle task testDebugUnitTestCoverage. After all of that, I'm able to generate test coverage report including connected/instrumentation Android tests and regular unit tests in java via sonar.
I am trying to generate Jacoco code coverage report.
I have used AndroidTestCase for my test classes.
I have found using testCoverageEnabled true and using default android -studio default jacoco,
./gradlew connectedCheck or createDebugCoverageReport
create the percentage of successfull/fail test cases, but no coverage report.
Then I have tried jacoco {toolVersion "0.7.1.201405082137"}, and task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug"). I have tried to change the dependsOn value with various task. The report shows 0 (zero) test coverage, which is impossible because at least half of all classes are tested.
I have followed various accepted answer of stack overflow in last couple of days. The result is negative.
My gradle file:
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "test.gradle.com.myapplicationtestgradle"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(
'proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
jacoco {
version "0.7.1.201405082137"
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
jacoco {
toolVersion "0.7.1.201405082137"
}
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
// exclude auto-generated classes and tests
def fileFilter = ['**/R.class', '**/R$*.class',
'**/BuildConfig.*', '**/Manifest*.*',
'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])
additionalSourceDirs = files([
"${buildDir}/generated/source/buildConfig/debug",
"${buildDir}/generated/source/r/debug"
])
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec', '**/*.ec'])
reports {
xml.enabled = true
xml.destination = "${buildDir}/jacocoTestReport.xml"
csv.enabled = false
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
}
Gradle already has built-in support for generating test coverage reports and we don't need to create any additional configurations or add any plugins to generate test coverage report. Basically, the only thing we need to do is to set testCoverageEnabled parameter to true in build.gradle file as follows:
android {
buildTypes {
debug {
testCoverageEnabled = true
}
}
}
Next, we can execute the following Gradle task from CLI:
./gradlew createDebugCoverageReport
On Windows, we can execute it like this:
gradlew.bat createDebugCoverageReport
Task will analyze code of our project in /src/main/java/ directory and unit tests placed in /src/androidTest/java/ directory.
After executing this task, we can find test coverage report in the following directory of the module:
/build/outputs/reports/coverage/debug/
When we open index.html file, we can see a visual report from test coverage, which can be viewed in a web browser.
It looks as on the image below.
I've written an article about test coverage report in Android application and published it on my blog some time ago. If you are interested in that topic, you can read it at:
https://web.archive.org/web/20171210224809/http://blog.wittchen.biz.pl/test-coverage-report-for-android-application (Old blog, with user comments)
http://wittchen.io/test-coverage-in-android-applications/ (New blog, without user comments)
Update (Unit test coverage for Robolectric tests)
If you would like to create test coverage report from unit tests written with Robolectric and generate test coverage report with Jacoco, Travis CI and Codecov.io, please take a look at the new article on my blog:
https://web.archive.org/web/20171013015925/http://blog.wittchen.biz.pl/test-coverage-report-with-travis-ci-for-robolectric-tests-on-android/ (Old blog)
http://wittchen.io/unit-test-coverage-report-with-travis-ci-for-robolectric-on-android/ (New blog, without user comments)
I see that you already got it working, however, there's a simpler method for getting Unit Test execution data. I recently was looking into this as well, I actually made a full write up earlier today.
In my situation, I didn't want to create an additional Gradle task as I wanted the report to be generated as a part of the existing workflow. I also didn't want to explicitly add the Jacoco plugin, as Google already dups the Jacoco Ant tasks for the coverage reports for Instrumentation Tests.
In addition to setting the properties android.jacoco.version and buildTypes.debug.testCoverageEnabled, I added the following to the testDebug JVM arguments to generate execution data:
project.afterEvaluate {
def append = "append=true"
def destFile = "destfile=$buildDir/outputs/code-coverage/connected/coverage.ec"
testDebug.jvmArgs "-javaagent:$buildDir/intermediates/jacoco/jacocoagent.jar=$append,$destFile"
createDebugCoverageReport.dependsOn testDebug
}
This appends the Unit Test execution data to the coverage file generated by connectedAndroidTest, so your report reflects both Instrumentation Tests and Unit Tests, rather than each variant individually.
Note that connectedAndroidTest overwrites the coverage file, take this into account when creating your report. If the task testDebug doesn't have any changes, and you run createDebugCoverageReport, it will only reflect your Instrumentation Test coverage. So, make a change to your Unit Tests. The Linux command touch may be useful here, although I haven't tried yet.
Today I did completely removed android studio, android sdk, gradle. Then reinstall everything. After that, I just added inside the app build.gradle.
debug {
testCoverageEnabled true
}
Then I run ./gradlew connectedChec. Everything is working perfectly. Android studio default Jacoco working fine for me. I think it is also possible to create a jacocoTestReport Task and then create code coverage.I don't know why gradle and android studio was not working previously.
i investigated correct way to get merged code coverage report with Jacoco, you can check it here: https://gist.github.com/ultraon/54cca81ca159ed0a4a9ebf62e89c26ba
I used AndroidStudio 2.2.2 with Gradle 3.2 and Android Gradle Plugin 2.2.2
I had the same problem too, but I made a mistake in the process which took a few days to track down. Here's my technique and the gotcha that caused the 0% coverage.
Install jacoco. I used the technique described in great detail here with a few modifications because of my project's structure. Thank you Ashish Uniyal!
Run jacoco. Open the Gradle window in Android Studio (View -> Tool Windows -> Gradle), click on the elephant icon to "Execute Gradle Task."
If a pop-up to your settings occurs, go ahead and disable the "Do not build Gradle task list during Gradle sync."
In the text window that pops up after clicking the elephant icon, type in: gradle app:createDebugUnitTestCoverageReport and hit [Enter].
If you want to do AndroidTests, type gradle app:createDebugAndroidTestCoverageReport.
It'll take several seconds to a few minutes to execute jacoco.
Find the results. This can vary depending on your settings in the jacoco.gradle file. But generally you can find the results an index.html file here: ./app/build/reports/coverage/test/debug/index.html.
My big gotcha was that I typed in the AndroidTest instead of the UnitTest (thank you auto-complete, sigh). Since I only did unit testing all jacoco could see was a big fat 0%.