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 22, 2021
1 parent 06932b4 commit 1309b4c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -304,6 +304,7 @@ kover {
jacocoEngineVersion.set("0.8.7") // change version of JaCoCo agent and reporter
generateReportOnCheck.set(true) // false to do not execute `koverReport` task before `check` task
disabledModules = setOf() // setOf("module-name") to disable coverage for module with name `module-name`
instrumentAndroidPackage = false // true to instrument packages `android.*` and `com.android.*`
}
```
</details>
Expand All @@ -319,6 +320,7 @@ kover {
jacocoEngineVersion.set('0.8.7') // change version of JaCoCo agent and reporter
generateReportOnCheck.set(true) // false to do not execute `koverReport` task before `check` task
disabledModules = [] // ["module-name"] to disable coverage for module with name `module-name`
instrumentAndroidPackage = false // true to instrument packages `android.*` and `com.android.*`
}
```
</details>
Expand Down
31 changes: 28 additions & 3 deletions src/main/kotlin/kotlinx/kover/KoverPlugin.kt
Expand Up @@ -4,6 +4,7 @@

package kotlinx.kover

import kotlinx.kover.adapters.*
import kotlinx.kover.api.*
import kotlinx.kover.api.KoverPaths.HTML_AGG_REPORT_DEFAULT_PATH
import kotlinx.kover.api.KoverNames.CHECK_TASK_NAME
Expand Down Expand Up @@ -264,7 +265,18 @@ class KoverPlugin : Plugin<Project> {
else
null
})
jvmArgumentProviders.add(CoverageArgumentProvider(this, agents, providers.koverExtension))

val excludeAndroidPackages =
project.provider { project.androidPluginIsApplied && !providers.koverExtension.get().instrumentAndroidPackage }

jvmArgumentProviders.add(
CoverageArgumentProvider(
this,
agents,
providers.koverExtension,
excludeAndroidPackages
)
)

doLast(IntellijErrorLogChecker(taskExtension))
}
Expand Down Expand Up @@ -293,8 +305,8 @@ private class IntellijErrorLogChecker(private val taskExtension: KoverTaskExtens
private class CoverageArgumentProvider(
private val task: Task,
private val agents: Map<CoverageEngine, CoverageAgent>,
@get:Nested
val koverExtension: Provider<KoverExtension>
@get:Nested val koverExtension: Provider<KoverExtension>,
@get:Input val excludeAndroidPackage: Provider<Boolean>
) : CommandLineArgumentProvider, Named {

@get:Nested
Expand All @@ -318,6 +330,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
*/
taskExtensionValue.excludes = taskExtensionValue.excludes + "android.*" + "com.android.*"
}

return agents.getFor(koverExtensionValue.coverageEngine.get()).buildCommandLineArgs(task, taskExtensionValue)
}
}
Expand Up @@ -17,3 +17,8 @@ internal fun createAdapters(): List<CompilationPluginAdapter> {
KotlinAndroidPluginAdapter()
)
}

val Project.androidPluginIsApplied: Boolean
get() {
return plugins.findPlugin("android") != null || plugins.findPlugin("kotlin-android") != null
}
6 changes: 6 additions & 0 deletions src/main/kotlin/kotlinx/kover/api/KoverExtension.kt
Expand Up @@ -45,6 +45,12 @@ open class KoverExtension(objects: ObjectFactory) {
*/
@get:Input
var disabledModules: Set<String> = emptySet()

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

public enum class CoverageEngine {
Expand Down

0 comments on commit 1309b4c

Please sign in to comment.