Skip to content

Commit

Permalink
Added property to exclude Android classes from the instrumentation
Browse files Browse the repository at this point in the history
Fixes #89
  • Loading branch information
shanshin committed Dec 13, 2021
1 parent 1475b5d commit 689c19c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/kotlin/kotlinx/kover/KoverPlugin.kt
Expand Up @@ -19,6 +19,7 @@ import kotlinx.kover.engines.intellij.*
import kotlinx.kover.engines.jacoco.*
import kotlinx.kover.tasks.*
import org.gradle.api.*
import org.gradle.api.provider.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.testing.*
import org.gradle.process.*
Expand Down Expand Up @@ -230,11 +231,16 @@ class KoverPlugin : Plugin<Project> {
else
null
})

val excludeAndroidPackage =
project.provider { project.androidPluginIsApplied && !koverExtension.instrumentAndroidPackage }

jvmArgumentProviders.add(
CoverageArgumentProvider(
jacocoAgent,
intellijAgent,
this,
excludeAndroidPackage,
koverExtension,
taskExtension
)
Expand All @@ -248,6 +254,7 @@ private class CoverageArgumentProvider(
private val jacocoAgent: JacocoAgent,
private val intellijAgent: IntellijAgent,
private val task: Task,
@get:Input val excludeAndroidPackage: Provider<Boolean>,
@get:Nested val koverExtension: KoverExtension,
@get:Nested val taskExtension: KoverTaskExtension
) : CommandLineArgumentProvider, Named {
Expand All @@ -262,6 +269,19 @@ private class CoverageArgumentProvider(
return mutableListOf()
}

if (excludeAndroidPackage.get()) {
/*
The instrumentation of android classes often causes errors when using third-party
frameworks (see https://github.com/Kotlin/kotlinx-kover/issues/89).
Because android classes are not part of the project, in any case they do not get into the report,
and they can be excluded from instrumentation.
FIXME Remove this code if the IntelliJ Agent stops changing project classes during instrumentation
*/
taskExtension.excludes = taskExtension.excludes + "android.*" + "com.android.*"
}

return if (koverExtension.coverageEngine.get() == CoverageEngine.INTELLIJ) {
intellijAgent.buildCommandLineArgs(taskExtension, task)
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/kotlinx/kover/adapters/PluginAdapter.kt
Expand Up @@ -18,6 +18,12 @@ private fun createAdapters(): List<CompilationPluginAdapter> {
)
}

val Project.androidPluginIsApplied: Boolean
get() {
return plugins.findPlugin("android") != null || plugins.findPlugin("kotlin-android") != null
}


fun Project.collectDirs(): Pair<FileCollection, FileCollection> {
val srcDirs = HashMap<String, File>()
val outDirs = HashMap<String, File>()
Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/kotlinx/kover/api/KoverExtension.kt
Expand Up @@ -39,6 +39,12 @@ open class KoverExtension(objects: ObjectFactory) {
*/
@get:Input
val generateReportOnCheck: Property<Boolean> = objects.property(Boolean::class.java)

/**
* Specifies whether the .classes from 'android' and 'com.android' packages should be included
*/
@get:Input
public var instrumentAndroidPackage: Boolean = false
}

public enum class CoverageEngine {
Expand Down

0 comments on commit 689c19c

Please sign in to comment.