i have to set up code coverage report on my android tests and then publish them in sonar. I have read that there are no tools and plugins which can do it. I am using gradle scripts and i try jacoco plugin, cobertura, but no results. Is it any ways to resolve it? Also i tried to do like here Gradle jacoco code coverage - Then publish/show in Jenkins
1 Answer
Example of build.gradle with code-coverage and sonar
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId 'com.example.coverage'
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName '1.0'
testHandleProfiling true
testFunctionalTest true
}
buildTypes {
debug {
testCoverageEnabled true
}
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
apply plugin: 'sonar-runner'
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/services/javax.annotation.processing.Processor'
exclude 'LICENSE.txt'
}
}
sonarRunner {
sonarProperties {
property "sonar.projectKey", "coverage-example"
property "sonar.projectName", "Coverage Example"
property "sonar.projectVersion", "1.0"
property "sonar.sources", "src/main/java"
property "sonar.binaries", "build"
property "sonar.test", "src/androidTest/java"
property "sonar.profile", "Sonar way"
property "sonar.language", "java"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.junit.reportsPath", "build/outputs/reports/coverage/debug"
property "sonar.cobertura.reportPath", "build/outputs/reports/coverage/debug/cobertura.xml"
property "sonar.java.coveragePlugin", "cobertura"
property "sonar.host.url", "http://localhost:9099"
}
}
Now run
gradlew clean assembleDebug createDebugCoverageReport
Now you will be able to see a xml containing the code coverage report in this directory
app/build/outputs/reports/coverage/debug/report.xml
Now before we run the sonar task, we have to convert the report.xml something that sonar can read, it will use this done in python script that performs the conversion of this file to the format that the plugin sonar cobertura can use it.
python cover2cover.py app/build/outputs/reports/coverage/debug/report.xml src/main/java > app/build/outputs/reports/coverage/debug/cobertura.xml && cp app/build/outputs/androidTest-results/connected/* app/build/outputs/reports/coverage/debug/
Now run
gradlew sonarRunner