Android Gradle Code Coverage
Asked Answered
P

2

9

I have a simple android project with test cases.

ProjNameProject
--build.gradle
--ProjName
----build.gradle

I see that by default android's new build system provides basic test results by default. (Hooray!)

Now I want to see code coverage as well. I know how to set this up using Emma and Ant scripts, however I don't want to run Ant scripts here. I feel that would defeat the purpose of me using the new build system.

I've tried a few Cobertura plugins that were found on Github. One in particular: https://github.com/stevesaliman/gradle-cobertura-plugin

However if I try to use the plugin in the ProjName build file then I get errors about the java plugin. I read on tools.android.com that adding the java plugin will generate this behavior. I'm not applying it so the cobertura plugin must be.
If I try to use the plugin in the main build file then I don't see the java errors but now i see:

Could not find net.sourceforge.cobertura:cobertura:1.9.4.1.
    Required by:
        :ProjNameProject:unspecified

What do I do??

Paradrop answered 14/6, 2013 at 3:54 Comment(3)
has anyone tried using clover gradle plugin in an android project?Melinite
possible duplicate of How to get code coverage using Android Studio?Lavery
The linked question was asked about three months after mine... So in reality that is the potential duplicate of mine.Paradrop
T
7

JaCoCo support was added to the Android gradle plugin v0.10 (http://tools.android.com/tech-docs/new-build-system).

Enable in the tested Build Type with testCoverageEnabled = true

android {
  jacoco {
    version = '0.6.2.201302030002'
  }
}

I was able to get JaCoCo coverage working with Robolectric by following http://chrisjenx.com/gradle-robolectric-jacoco-dagger/.

apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'jacoco'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:appcompat-v7:19.1.+'

    androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
    androidTestCompile 'junit:junit:4.11'
    androidTestCompile 'org.robolectric:robolectric:2.3'
    androidTestCompile 'com.squareup:fest-android:1.0.+'
}

robolectric {
    // Configure the set of classes for JUnit tests
    include '**/*Test.class'
    exclude '**/*AbstractRobolectricTestCase.class'

    // Configure max heap size of the test JVM
    maxHeapSize = "2048m"
}

jacoco {
    toolVersion = "0.7.1.201405082137"
}

//Define coverage source.
//If you have rs/aidl etc... add them here.
def coverageSourceDirs = [
    'src/main/java',
    'src/gen'
]

...

// Add JaCoCo test reporting to the test task
// http://chrisjenx.com/gradle-robolectric-jacoco-dagger/
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }

    // Class R is used, but usage will not be covered, so ignore this class from report
    classDirectories = fileTree(
        dir: './build/intermediates/classes/debug',
        excludes: ['**/R.class',
                   '**/R$*.class'
    ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebug.exec')
}
Timeconsuming answered 30/7, 2014 at 13:11 Comment(1)
see improved script gist.github.com/ultraon/54cca81ca159ed0a4a9ebf62e89c26baMenis
F
6

Emma support is planned to be released soon within the new Android build system : http://tools.android.com/tech-docs/new-build-system/roadmap

Up to now there is no official way to run emma with android via gradle. I guess instrumenting can be achieved pretty easily but then, you will miss a way to tell Android to run tests with coverage on. Moreover, there is currently no way (to the best of my knowledge) to pull down emma runtime coverage data from the device.

This project can interest you : https://github.com/stephanenicolas/Quality-Tools-for-Android. It will be updated as soon as emma will make it into Android Gradle plugin.

----UPDATE

This plugin has no chance to work with Android has it uses the Java plugin which is incompatible with Android plugin.

Frons answered 13/8, 2013 at 23:8 Comment(2)
While the Emma plugin for Gradle depends on the Java plugin (and thus does not work with the Android plugin), the JaCoCo plugin for Gradle does not seem to depend on the Java plugin and should work.Sherwood
You can find an example of Gradle build instrumented with Jacoco on the project QAT mentionned above. Last time I tried though, a regression prevented latest version of Jacoc from working but that might have been fixed.Frons

© 2022 - 2024 — McMap. All rights reserved.