jacoco code coverage not working for Android applications - android

My root build.gradle file
buildscript {
repositories {
google()
mavenCentral()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jacoco:org.jacoco.core:0.8.0"
}
}
apply plugin: 'android-sdk-manager'
allprojects {
apply plugin: 'jacoco'
apply plugin: 'com.github.ben-manes.versions'
repositories {
google()
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
}
tasks.withType(JavaCompile) {
sourceCompatibility = JavaVersion.VERSION_1_10
targetCompatibility = JavaVersion.VERSION_1_10
// report all Java errors even if the IDE does not
configure(options) {
compilerArgs << '-Xlint:all' << '-Xlint:-options'
deprecation = true
encoding = 'UTF-8'
}
}
// print errors from test in the terminal
tasks.withType(Test) {
testLogging {
exceptionFormat 'full'
}
}
project.plugins.whenPluginAdded { plugin ->
if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
println "Disable pre dexing for module ${project.name}"
project.android.dexOptions.preDexLibraries = false
} else if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
println "Disable pre dexing for module ${project.name}"
project.android.dexOptions.preDexLibraries = false
}
}
}
app/build.gradle file
apply plugin: 'com.android.application'
apply from: '../jacoco.gradle'
android {
compileSdkVersion COMPILE_SDK_VERSION as int
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId "..."
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
project.ext.set("archivesBaseName", "..." + versionName)
multiDexEnabled true
renderscriptTargetApi RENDERSCRIPT_TARGET_API as int
renderscriptSupportModeEnabled true
}
signingConfigs {
debugKey {
...
}
releaseKey {
...
}
}
buildTypes {
debug {
versionNameSuffix ''
debuggable true
minifyEnabled false
zipAlignEnabled true
signingConfig signingConfigs.debugKey
testCoverageEnabled true
}
staging {
versionNameSuffix ''
debuggable false
minifyEnabled true
zipAlignEnabled true
signingConfig signingConfigs.stagKey
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
matchingFallbacks = ['release']
}
release {
versionNameSuffix ''
debuggable false
minifyEnabled true
zipAlignEnabled true
signingConfig signingConfigs.releaseKey
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'LICENSE.txt'
}
testOptions {
animationsDisabled true
unitTests {
returnDefaultValues = true
includeAndroidResources = true
}
}
lintOptions {
lintConfig file('lint.xml')
abortOnError false
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
dexOptions {
javaMaxHeapSize "3g"
jumboMode = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// Espresso
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
// Junit
testImplementation "junit:junit:${JUNIT_VERSION}"
//Mockito
testImplementation "org.mockito:mockito-core:2.22.0"
...
}
and jacoco.gradle file
apply plugin: 'jacoco'
def buildDir = "$project.buildDir"
def taskName = "debug"
def testTaskName = "testDebugUnitTest"
jacoco {
toolVersion "0.7.7.201606060606"
}
task "${testTaskName}Coverage"(type: JacocoReport, dependsOn: ["${testTaskName}", "createDebugCoverageReport"]) {
group = "Reporting"
description = "Generate Jacoco coverage reports on the ${testTaskName} build."
reports {
xml.enabled = true
html.enabled = true
html.destination file("$buildDir/jacoco")
csv.enabled false
}
def coverageSourceDirs = ["src/main/java"]
def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*',
'android/**',
'com/google/**',
'com/intellij/**',
'junit/**',
'net/**',
'okhttp/**',
'org/**',
'rx/**',
'**/*_MembersInjector.class',
'**/Dagger*Component.class', // covers component implementations
'**/Dagger*Component$Builder.class', // covers component builders
'**/*Module_*Factory.class'
]
def javaClasses = fileTree(
dir: "$buildDir/intermediates/app_classes/${taskName}",
excludes: fileFilter
)
classDirectories = files([javaClasses])
sourceDirectories = files([coverageSourceDirs])
additionalSourceDirs = files([coverageSourceDirs])
executionData = fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec"
])
}
and I am running following command on mac
./gradlew clean testDebugUnitTestCoverage
When I run this command, it shows message
Task ':app:testDebugUnitTestCoverage' is not up-to-date because:
Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/index.html has been removed.
Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/jacoco-sessions.html has been removed.
Output property 'reports.enabledDirectoryReportDestinations.html' file .../app/build/jacoco/jacoco-resources has been removed.
[ant:jacocoReport] Loading execution data file .../app/build/jacoco/testDebugUnitTest.exec
[ant:jacocoReport] Writing bundle 'app' with 3040 classes
and no coverage gets calculated in folder
reports/coverage/debug
however folder
tests/testDebugUnitTest
shows passed unit tests data with success.

I found solution here.
Just change path of classes directory
def javaClasses = fileTree(
dir: "$buildDir/intermediates/javac/debug",
excludes: fileFilter
)

I wrote an article a while ago about jacoco.
https://github.com/uriel-frankel/android-code-coverage/
The jacoco version should change as well:
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.7.5.201505241946'
}

Related

Failed to load library from git in androidStudio

I'm trying to barteksc/AndroidPdfViewer in my android project but it fail to load the lib and I can't figure out why... Any Idea?
Here's my config :
build.gradle (:app)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'jacoco'
id 'com.google.gms.google-services'
id 'dagger.hilt.android.plugin'
}
apply plugin: 'kotlin-kapt'
android {
compileSdk 31
defaultConfig {
applicationId "com.github.multimatum_team.multimatum"
minSdk 26
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.github.multimatum_team.multimatum.MultimatumTestRunner"
}
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'
}
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
testCoverage {
jacocoVersion = "0.8.7"
}
}
dependencies {
implementation fileTree(dir: "libs", include:["*.jar"])
implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
implementation 'com.google.code.gson:gson:2.8.9'
implementation "androidx.preference:preference-ktx:1.2.0"
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
....
testImplementation 'org.mockito:mockito-core:4.4.0'
testImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1'
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
jacoco.includes = ["com.application.*"]
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled true
html.enabled true
}
def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
// Exclude Hilt generated classes
'**/*Hilt*.*',
'hilt_aggregated_deps/**',
'**/*_Factory.class',
'**/*_MembersInjector.class'
]
def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
def mainSrc = "$project.projectDir/src/main/java"
sourceDirectories.setFrom(files([mainSrc]))
classDirectories.setFrom(files([debugTree]))
executionData.setFrom(fileTree(dir: project.buildDir, includes: [
'outputs/unit_test_code_coverage/debugUnitTest/testDebugUnitTest.exec',
'outputs/code_coverage/debugAndroidTest/connected/*/coverage.ec'
]))
}
connectedCheck {
finalizedBy jacocoTestReport
}
// Allow references to generated code
kapt {
correctErrorTypes = true
}
build.gradle (project
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories{
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
classpath("com.google.dagger:hilt-android-gradle-plugin:2.38.1")
}
}
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
error : "Could not resolve com.github.barteksc:android-pdf-viewer:3.2.0-beta.1."
I haven't found why I cannot load a library from github. Do I have to use something like jitpack?
I cannot use maven since the lib doesn't support it.
You can't just link to code in github like that. It needs to be packaged into a library and downloaded in compiled form. You have to use maven or similar repository. Gradle doesn't even know how to download source code from git for a dependency, it only expects packaged code.
I wouldn't use this library in any case. It hasn't been updated in 3 years, and the first line of its readme is that its looking for a new maintainer. And since it has no updates, it didn't find one. I would be vary wary of some random library on github, and doubly wary of one that's not maintained.

Jacoco shows 0% coverage even though 100% tests are passing

I have been working on an android project in kotlin where I have a few packages: app, package1, package2, package3 each package has its own build.gradle like build.gradle for app, package1, package2, package3 in the respective package.
There is also a top-level build.gradle. Now each package has a src and a tests folder. Now I have created a jacoco.gradle file in the package.root. and applied apply from: "$project.rootDir/jacoco.gradle" in each package's build.gradle.
Here is my jacoco.gradle file for reference.
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.6"
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
project.afterEvaluate {
(android.hasProperty('applicationVariants')
? android.'applicationVariants'
: android.'libraryVariants').all { variant ->
def variantName = variant.name
def unitTestTask = "test${variantName.capitalize()}UnitTest"
def uiTestCoverageTask = "create${variantName.capitalize()}CoverageReport"
tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
"$unitTestTask",
"$uiTestCoverageTask"
]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"
reports {
html.enabled = true
xml.enabled = false
csv.enabled = false
}
def fileFilter = [
// data binding
'android/databinding/**/*.class',
'**/android/databinding/*Binding.class',
'**/android/databinding/*',
'**/androidx/databinding/*',
'**/BR.*',
// android
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
// kotlin
'**/*MapperImpl*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*Component*.*',
'**/*BR*.*',
'**/Manifest*.*',
'**/*$Lambda$*.*',
'**/*Companion*.*',
'**/*Module*.*',
'**/*Dagger*.*',
'**/*MembersInjector*.*',
'**/*_MembersInjector.class',
'**/*_Factory*.*',
'**/*_Provide*Factory*.*',
'**/*Extensions*.*',
// sealed and data classes
'**/*$Result.*',
'**/*$Result$*.*'
]
classDirectories.setFrom(files([
fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: fileFilter)
]))
def variantSourceSets = variant.sourceSets.java.srcDirs.collect { it.path }.flatten()
sourceDirectories.setFrom(project.files(variantSourceSets))
def uiTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])
executionData(files([
"$project.buildDir/jacoco/${unitTestTask}.exec",
uiTestsData
]))
}
}
}
Now the problem is when I run the testDebugUnitTestCoverage task even though all tests present in a package pass the results show 0% code coverage and all lines are missed. Can someone help me find a solution?
Update
The coverage is reported with app package and package2(without any change in any files) but jacoco still reports 0 coverage for package1 and package3
Edit
added gradle file for 1 of the failing package
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply from: "$project.rootDir/jacoco.gradle"
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath true
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
testOptions {
unitTests {
includeAndroidResources = true
all {
forkEvery = 1
maxParallelForks = Runtime.getRuntime().availableProcessors()
testLogging {
events "passed", "skipped", "failed"
showExceptions = true
exceptionFormat = "full"
showCauses = true
showStackTraces = true
showStandardStreams = false
}
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(
'androidx.appcompat:appcompat:1.0.2',
'androidx.exifinterface:exifinterface:1.0.0-rc01',
'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha03',
'androidx.work:work-runtime-ktx:2.4.0',
'com.google.dagger:dagger:2.24',
'com.google.firebase:firebase-analytics-ktx:17.5.0',
'com.google.firebase:firebase-crashlytics:17.0.0',
"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
)
testImplementation(
'android.arch.core:core-testing:1.1.1',
'androidx.test.espresso:espresso-core:3.2.0',
'androidx.test.ext:junit:1.1.1',
'androidx.work:work-testing:2.4.0',
'com.google.dagger:dagger:2.24',
'com.google.truth:truth:0.43',
'junit:junit:4.12',
"org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version",
'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2',
'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.2.2',
'org.mockito:mockito-core:2.19.0',
'org.robolectric:robolectric:4.3',
project(":package2"),
)
kapt(
'com.google.dagger:dagger-compiler:2.24',
)
kaptTest(
'com.google.dagger:dagger-compiler:2.24',
)
api project(':package1')
implementation project(':package4')
implementation project(':package3')
}
repositories {
mavenCentral()
}
configurations {
all*.exclude module: 'protobuf-java'
}
update
when I run coverage from run configurations in android studio I get the correct data but when I run it from gradle tasks it shows 0 coverage.

Proguard error while signing APK with Android Studio

I am trying to debug my error with Proguard. My project is working fine with debug but not with Proguard. Any help will be appreciated.
I have tried with ignore warning in Proguard. However application is crashing with generated APK.
Current Proguard settings is not working. Messages console I have uploaded to Gist
Build.gradle is given below
Prodguard-project.txt in gist
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}
build.gradle
apply plugin: 'com.android.application'
apply from: rootProject.file('gradle/codequality.gradle')
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
versionCode 49
versionName "1.8.8"
minSdkVersion 14
targetSdkVersion 22
buildConfigField 'String', 'BUILD_TAG', '"' + getBuildTag() + '"'
buildConfigField 'String', 'OWM_API_KEY', '"' + getOpenWeatherMapApiKey() + '"'
buildConfigField 'boolean', 'ENABLE_WEATHER', 'true'
def buildSuffix = getBuildSuffix(versionName, versionCode)
applicationVariants.all { variant ->
def file = variant.outputs[0].outputFile
variant.outputs[0].outputFile = new File(file.parent, file.name.replace(".apk", "-" + buildSuffix + ".apk"))
}
}
if (project.hasProperty('signingKeyStoreFile')) {
signingConfigs {
release {
storeFile file(signingKeyStoreFile)
storePassword signingKeyStorePassword
keyAlias signingKeyAlias
keyPassword signingKeyPassword
}
}
}
buildTypes {
release {
minifyEnabled true
proguardFile 'proguard-project.txt'
if (project.hasProperty('signingKeyStoreFile')) {
signingConfig signingConfigs.release
}
}
}
lintOptions {
abortOnError false
checkReleaseBuilds false
disable 'MissingTranslation'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
def getBuildSuffix(versionName, versionCode) {
def suffix = versionName + '-' + versionCode
if (System.getenv()['BUILD_NUMBER'] != null) {
suffix += '-b' + System.getenv()['BUILD_NUMBER']
}
return suffix
}
def getBuildTag() {
def tag = ''
if (System.getenv()['BUILD_NUMBER'] != null) {
tag += 'b' + System.getenv()['BUILD_NUMBER']
} else {
tag += 'l'
}
tag += '#' + new Date().format('yyyyMMdd')
return tag
}
def getOpenWeatherMapApiKey() {
if (project.hasProperty('owmApiKey')) {
return owmApiKey
} else {
def apiKeyFile = file('default_owm_api_key');
if (apiKeyFile.isFile()) {
return apiKeyFile.text.trim()
}
}
return 'NOKEY'
}
///////////////////////////////////////////////////
// Dependencies
repositories {
mavenCentral()
}
dependencies {
//compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
//compile 'com.android.support:support-v4:19.1.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'net.danlew:android.joda:2.9.4.1'
compile 'com.google.android.gms:play-services-ads:9.8.0'
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
}
configurations {
all*.exclude group: 'com.google.firebase', module: 'firebase-core'
all*.exclude group: 'com.google.firebase', module: 'firebase-iid'
}
///////////////////////////////////////////////////
// Checkstyle
task checkstyleDebug(type: Checkstyle, dependsOn: 'compileDebugSources') {
source = fileTree('src/main/java/')
classpath = files('build/intermediates/classes/debug')
}
check.dependsOn checkstyleDebug
///////////////////////////////////////////////////
// Findbugs
task findbugsDebug(type: FindBugs, dependsOn: 'compileDebugSources') {
source = fileTree('src/main/java/')
classes = fileTree('build/intermediates/classes/debug')
classpath = files() // empty classpath!
effort = 'max'
excludeFilter = rootProject.file('config/findbugs/androidExcludeFilter.xml')
}
check.dependsOn findbugsDebug
///////////////////////////////////////////////////
// PMD
task pmd(type: Pmd) {
source = fileTree('src/main/java/')
ruleSets = ['java-basic', 'java-braces', 'java-android']
}
check.dependsOn 'pmd'
Try to add getDefaultProguardFile('proguard-android.txt') to the proguardFile 'proguard-project.txt', to get next row:
proguardFile getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
Or start to search the issues from your proguard-project.txt, like:
Add rule -keepattributes SourceFile,LineNumberTable,InnerClasses,Signature,Exceptions
Add rule -dontwarn com.google.**
And so on, by your proguard failed logs

Running Checker Framework on Android

I want to use Checker Framework to do some static analysis of my app- checking for Nullability failures, UI constraint failures, etc at runtime. I followed the instructions at https://checkerframework.org/manual/#android-gradle to try and make it run, but I'm getting an error that checkTypes is not a task. I think I followed the instructions correctly, and I fixed the capitalization mismatch they had. ANy ideas how to fix it? My build.gradle is below:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
google()
}
apply plugin: 'jacoco'
apply plugin: 'com.getkeepsafe.dexcount'
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xmaxerrs" << "500"
}
}
android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.handshake.hsdm"
minSdkVersion 19
targetSdkVersion 25
versionCode 35
versionName "0.0.35"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testCoverageEnabled = false
}
debug {
testCoverageEnabled = false
}
checkTypes {
javaCompileOptions.annotationProcessorOptions.
classNames.add("org.checkerframework.checker.guieffect.GuiEffectChecker")
// You can pass options like so:
// javaCompileOptions.annotationProcessorOptions.arguments.put("warns", "")
}
}
dexOptions {
preDexLibraries = false
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
packagingOptions {
//These files constantly step on each other from multiple libraries, don't include them
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/*'
exclude 'plugin.xml'
exclude 'plugin.properties'
exclude 'about_files/LICENSE-2.0.txt'
}
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
jacoco {
version = '0.7.3.201502191951'
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations {
checkerFrameworkAnnotatedJDK {
description = 'a copy of JDK classes with Checker Framework type qualifers inserted'
}
}
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',
'com/handshake/hsdm/demo/**','com/handshake/hsdm/localstorage/schema1/*Dao*',
'com/handshake/hsdm/localstorage/schema1/*Factory*', 'com/handshake/hsdm/dagger2',
'**/*Module*', '**/*_Factory*', '**/*_MembersInjector*'
]
def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
executionData = fileTree(dir: "$buildDir", includes: [
"jacoco/testDebugUnitTest.exec",
"outputs/code-coverage/connected/*coverage.ec"
])
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: ['com/handshake/hsdm/demo/**'])
})
}
}
dependencies {
//Clipping dependencies
ext.checkerFrameworkVersion = '2.2.1'
implementation "org.checkerframework:checker-qual:${checkerFrameworkVersion}"
annotationProcessor "org.checkerframework:checker:${checkerFrameworkVersion}"
checkerFrameworkAnnotatedJDK "org.checkerframework:jdk8:${checkerFrameworkVersion}"
}
apply plugin: 'com.google.gms.google-services'
gradle.projectsEvaluated {
tasks.withType(JavaCompile).all { compile ->
if (compile.name.contains("checkTypes")) {
compile.options.compilerArgs += [
"-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
]
}
}
}
Edit: For future readers- the line javaCompileOptions.annotationProcessorOptions.classNames +=["org.checkerframework.checker.nullness.NullnessChecker"] seems to be the problem. removing it runs dagger and other annotation processors, but obviously won't run the checker.
So apparently if you specify 1 annotation processor via classnames, it overwrites any others. So I had to specify dagger and autofactory as well. Then I can either run it from Android Studio as a build, or from command line as:
gradlew assembleCheckTypes

Jacoco Android createDebugCoverageReport not found

I want to run my tests in Android app and create coverage reports, so I added Jacoco configuration into my build.gradle file, but it doesn't work.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "mm"
minSdkVersion 12
targetSdkVersion 18
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile 'com.google.code.gson:gson:2.1'
compile files('libs/android-async-http-1.4.4.jar')
compile files('libs/freemarker.jar')
compile files('libs/greendao-1.3.1.jar')
compile files('libs/raygun4android-1.1.0.jar')
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.hamcrest:hamcrest-library:1.3'
compile 'junit:junit:4.11'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
}
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
jacoco {
version "0.7.1.201405082137"
}
jacoco {
toolVersion "0.7.1.201405082137"
}
def coverageSourceDirs = [
'src/main/java',
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/jacoco/testDebug.exec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
I know, there is issue with gradle version 1.3.0 and with 1.3.1 it should work normally, however with 1.3.1 I get Task 'createDebugCoverageReport' not found in root project.
You have to enable the testCoverageEnabled :
buildTypes {
debug{
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
isTestCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
isTestCoverageEnabled true
}
}

Categories

Resources