Integrate code coverage into android plugin in gradle script


(Vladimir Voitekhovski) #1

Hi, with which tools can i made code coverage and after that push it to sonar in gradle script? I have got android Unit tests, and android plugin turn on in my build.gradle, when i try to include jacoco plugin, i have got a conflict. Is there any ways to apply jacoco or use another tools for code coverage in gradle using android plugin? if it will help to resolve my issue, i can sen my build.gradle. Really need help.


(Radim Kubacki) #2

Hi Vladimir,

you can get better answer at https://groups.google.com/d/forum/adt-dev where new android build system is discussed or you can check the existing documentation at http://tools.android.com/tech-docs/new-build-system

Android plugin is under heavy development and as far as I know there are several different attempts to extend its test support that only deals with instrumentation tests and add unit test there. I do not know if they integrate with jacoco correctly or not. BTW: you haven’t told us how you unit-test your application.


(Vladimir Voitekhovski) #3

Here is my buid gradle which run unit tests apply plugin: ‘android’

android {

compileSdkVersion 19

buildToolsVersion “19.0.1”

defaultConfig {

minSdkVersion 14

targetSdkVersion 19

versionCode 1

versionName “$version_code”

testPackageName “chem.android.test”

testInstrumentationRunner “android.test.InstrumentationTestRunner”

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

signingConfigs {

release {

storeFile file("…/config/signing/key.jks")

storePassword = “”

keyAlias = “”

keyPassword = “”

}

}

sourceSets {

instrumentTest {

java {

exclude ‘**/RegistrationActivityTest.java’

}

}

}

buildTypes {

release {

runProguard false

proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.txt’

signingConfig signingConfigs.release

}

android.applicationVariants.all{ variant ->

println variant.description + " artifact path **********";

//def variants = variant.baseName.split("-");

def apkName = “${group}”;

//apkName += variants[0];

//apkName += “-${version}”;

if (!variant.zipAlign) {

apkName += “-unaligned”;

}

apkName += “-” + revision;

apkName += “-” + defaultConfig.versionName;

apkName += “-” + variant.buildType.name;

apkName += “.apk”;

println “$project.buildDir/apk/” + apkName

variant.outputFile = file("$project.buildDir/apk/" + apkName)

}

lintOptions {

abortOnError false

}

packagingOptions {

exclude “META-INF/LICENSE.txt”

exclude “META-INF/NOTICE.txt”

exclude ‘LICENSE.txt’

}

} }

dependencies {

compile ‘com.android.support:support-v4:+’

compile ‘com.google.code.gson:gson:2.2.4’

compile ‘com.google.android.gms:play-services:+’

compile ‘com.squareup.retrofit:retrofit:1.4.1’

compile(‘org.simpleframework:simple-xml:2.7.+’){

exclude module: ‘stax’

exclude module: ‘stax-api’

exclude module: ‘xpp3’

}

compile ‘com.vol:library:1.0.+’

compile project(’:cool’)

compile project(’:milk’)

}

apply plugin: “sonar-runner”

sonarRunner {

sonarProperties {

property “sonar.host.url”, “”

property “sonar.login”, “”

property “sonar.password”, “”

property “sonar.jdbc.url”, “”

property “sonar.jdbc.driverClassName”, “”

property “sonar.jdbc.username”, “”

property “sonar.jdbc.password”, “”

property “sonar.profile”, “”

property “sonar.projectName”, “”

property “sonar.language”, “”

property “sonar.sources”, “src/main/java”

property “sonar.binaries”, “build”;

} }

configurations {

codequality }

dependencies {

codequality ‘com.puppycrawl.tools:checkstyle:5.6’ }

task checkstyle(type: AndroidCheckstyleTask) {

ignoreFailures true

showViolations false }

check.dependsOn(checkstyle)

/////////////////////////////////////////////// ////////////// Groovy Task Class ////////////// /////////////////////////////////////////////// import org.gradle.api.internal.project.IsolatedAntBuilder

/**

*/ class AndroidCheckstyleTask extends DefaultTask {

@InputFile @Optional File configFile = new File("$project.rootDir/config/checkstyle/AndroidStyle.xml")

@InputFile @Optional File xslFile = new File("$project.rootDir/config/checkstyle/checkstyle-noframes-sorted.xsl")

@OutputFile @Optional File outputFile = new File("$project.buildDir/reports/checkstyle/checkstyle-${project.name}.xml")

FileCollection checkstyleClasspath = project.configurations.codequality

Boolean ignoreFailures = false

Boolean showViolations = true

Project gradleProject = project

def AndroidCheckstyleTask() {

description = ‘Runs checkstyle against Android sourcesets.’

group = ‘Code Quality’

}

@TaskAction

def runCheckstyle() {

outputFile.parentFile.mkdirs()

def antBuilder = services.get(IsolatedAntBuilder)

antBuilder.withClasspath(checkstyleClasspath).execute {

ant.taskdef(name: ‘checkstyle’, classname: ‘com.puppycrawl.tools.checkstyle.CheckStyleTask’)

// see also, maxWarnings and failureProperty arguments

ant.checkstyle(config: configFile, failOnViolation: !ignoreFailures) {

fileset(dir: gradleProject.projectDir.getPath()) {

gradleProject.android.sourceSets.each { sourceSet ->

sourceSet.java.each { file ->

include(name: gradleProject.relativePath(file))

}

}

}

if (showViolations) {

formatter(type: ‘plain’, useFile: false)

}

formatter(type: ‘xml’, toFile: outputFile)

}

if (outputFile.exists()) {

ant.xslt(in:

outputFile,

style: xslFile,

out:

outputFile.absolutePath.replaceFirst(~/.[^.]+$/, “.html”)

)

}

}

} }


(Radim Kubacki) #4

There are many things done differently in Android project than in Java. The result is that you cannot simply apply jacoco plugin and get the result but have to wait until support is added to the instrumentation test runner (either for Emma or JaCoCo or anything else).

You can also check if some of unit test supports like Robolectric already integrates with code coverage and possibly use this.

FYI https://code.google.com/p/android/issues/detail?id=60964


(Vladimir Voitekhovski) #5

Thanks Radim, maybe you can get me some links where i can find an answer on “There are many things done differently in Android project than in Java.” because i’am new in gradle and java