question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code Coverage not reported for instrumented tests (Android library module) on AGP 7.0

See original GitHub issue

Hi I’m trying this on my test multi-module project before trying this on the other project I’m working on. I’ve followed the steps and run ./gradlew rootCoveragePlugin but got this report:

Screen Shot 2021-05-07 at 5 17 43 PM

– com.raquezha.heograpiya.splash is 0% but when I run it in Android Studio local coverage it recognize the tests:

Screen Shot 2021-05-07 at 5 59 14 PM

here’s the file structure as you can see Android Studio recognizes the local code coverage of splash module and says its 100% lines covered

Screen Shot 2021-05-07 at 6 08 57 PM

here’s my top-level build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    apply from: rootProject.file('dependencies.gradle')

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0-alpha15'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0'
        classpath 'com.google.gms:google-services:4.3.5'
        classpath 'nl.neotech.plugin:android-root-coverage-plugin:1.4.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id "nl.neotech.plugin.rootcoverage" version "1.4.0"
}

apply plugin: 'nl.neotech.plugin.rootcoverage'

jacoco {
    toolVersion = "0.8.7"
}

tasks.withType(Test) {
    useJUnitPlatform()
    testLogging {
        exceptionFormat "full"
        events "started", "skipped", "passed", "failed"
        showStandardStreams true
    }
}

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                compileSdk 30
                buildToolsVersion "30.0.3"

                defaultConfig {
                    minSdk 26
                    targetSdk 30
                    versionCode 1
                    versionName "1.0"

                    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                }

                buildTypes {
                    release {
                        minifyEnabled false
                        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                    }

                    debug {
                        testCoverageEnabled true
                    }
                }

                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_8
                    targetCompatibility JavaVersion.VERSION_1_8
                }

                kotlinOptions {
                    jvmTarget = '1.8'
                }

                buildFeatures {
                    viewBinding true
                }

                sourceSets {
                    main {
                        java.srcDirs = ['src/main/kotlin']
                    }
                    androidTest {
                        java.srcDirs = ['src/androidTest/kotlin']
                    }
                    test {
                        java.srcDirs = ['src/test/kotlin']
                    }
                }
            }
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

here’s the splash module build.gradle:

plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

dependencies {
    implementation _libs.lottie

    implementation project(':shared')

    testImplementation project(path: ':shared', configuration: 'testDependencies')
    androidTestImplementation project(path: ':shared', configuration: 'androidTestDependencies')
}

tasks.withType(Test) {
    useJUnitPlatform()
    testLogging {
        exceptionFormat "full"
        events "started", "skipped", "passed", "failed"
        showStandardStreams true
    }
}

I’m using:

  1. Android Studio Arctic Fox | 2020.3.1 Canary 15
  2. Gradle JDK: AdoptOpenj9-11
  3. gradle-wrapper: distributionUrl=https://services.gradle.org/distributions/gradle-7.0-bin.zip

I’m using canary because this is only a test project I want to test new stuff

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:24 (16 by maintainers)

github_iconTop GitHub Comments

14reactions
Rolf-Smitcommented, Nov 23, 2021

Update:

It seems Google finally stepped in and fixed the issue in AGP version 7.2.0: https://issuetracker.google.com/issues/195860510#comment12

Unfortunately the fix will not be released this month, they expect to release a beta version of AGP 7.2.0 that includes this fix around December 13th.

5reactions
Rolf-Smitcommented, Sep 21, 2021

Hi @raquezha took me a while to take a look at this, but I found a few things:

Looking at the report that is generated, it is missing all execution data, which is needed to calculate coverage. You can check this for your self by going to the sessions part of the report: Schermafbeelding 2021-06-04 om 13 36 30

This can have multiple causes, but in this case this happens because the Android Gradle Plugin updated where these files are located since version 7.0. This means the Plugin needs a small update.

However I also spotted this, which will also prevent coverage (even if I update the plugin):

In the root project build.gradle file you have this line:

getExecutionData().setFrom(fileTree(buildDir).include("/jacoco/*.exec"))

This basically overrides any of the default set execution data location’s with the location you provided. Your location only looks at the buildDir of the root project, but normally execution data is generated in all the different module buildDir’s, so this will definitely not work if you ask me.

TLDR: This plugin needs an update due to changes in the Android Gradle Plugin 7.0. I’m going to take care of this. (see update this is no longer something I can fix)

Read more comments on GitHub >

github_iconTop Results From Across the Web

AGP 7.0.0 does not create unit test jacoco exec files correctly ...
Now that AGP 7.0.0 is stable, here's the current behavior I'm seeing, which still doesn't seem correct: For library modules, with testCoverageEnabled true ......
Read more >
Unit tests not working on Android Studio Arctic Fox
Go to Toolbar > Run > Edit Configurations · Then create a new configuration under Gradle with tasks for the tests you want...
Read more >
Test in Android Studio
Android Studio is designed to make testing simple. It contains many features to simplify how you create, run, and analyze tests.
Read more >
Android code coverage using JaCoCo | by Radhika S - Canopas
2. Set up Jacoco configuration · variantName.capitalize()} · UnitTest" // Add unit test coverage tasks tasks. · testTaskName · Coverage", type: JacocoReport, ...
Read more >
Android User Guide - Google Samples
Try using the latest versions of lint (the Android Gradle plugin), as well as of Gradle — even if that means using a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found