kotlinx.kover.gradle.plugin.locators.Android.kt Maven / Gradle / Ivy
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.kover.gradle.plugin.locators
import kotlinx.kover.gradle.plugin.commons.AndroidBuildVariant
import kotlinx.kover.gradle.plugin.commons.AndroidFallbacks
import kotlinx.kover.gradle.plugin.commons.AndroidFlavor
import kotlinx.kover.gradle.plugin.commons.KoverIllegalConfigException
import kotlinx.kover.gradle.plugin.appliers.origin.AndroidVariantOrigin
import kotlinx.kover.gradle.plugin.appliers.origin.CompilationDetails
import kotlinx.kover.gradle.plugin.util.DynamicBean
import kotlinx.kover.gradle.plugin.util.bean
import kotlinx.kover.gradle.plugin.util.hasSuperclass
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.withType
/**
* Locate Android compilation kits for the given Kotlin Target.
*/
internal fun Project.androidCompilationKits(
androidExtension: DynamicBean,
kotlinTarget: DynamicBean
): List {
val variants = if ("applicationVariants" in androidExtension) {
androidExtension.beanCollection("applicationVariants")
} else {
androidExtension.beanCollection("libraryVariants")
}
val fallbacks = findFallbacks(androidExtension)
return variants.map {
extractAndroidKit(androidExtension, kotlinTarget, fallbacks, it)
}
}
private fun Project.extractAndroidKit(
androidExtension: DynamicBean,
kotlinTarget: DynamicBean,
fallbacks: AndroidFallbacks,
variant: DynamicBean
): AndroidVariantOrigin {
val variantName = variant.value("name")
val compilations = provider {
extractCompilation(kotlinTarget, variantName)
}
// if `unitTestVariant` not specified for application/library variant (is null) then unit tests are disabled for it
val unitTestVariantName = variant.beanOrNull("unitTestVariant")?.value("name")
val tests = tasks.withType().matching { test ->
unitTestVariantName != null
// use only Android unit tests (local tests)
&& test.hasSuperclass("AndroidUnitTest")
// only tests of current application build variant
&& test.bean().value("variantName") == unitTestVariantName
}
val buildTypeName = variant["buildType"].value("name")
val flavors = variant.beanCollection("productFlavors").map { flavor ->
val flavorName = flavor.value("name")
val dimension = flavor.valueOrNull("dimension")
?: throw KoverIllegalConfigException("Product flavor '$flavorName' must have at least one flavor dimension. Android Gradle Plugin with version < 3.0.0 not supported")
AndroidFlavor(dimension, flavorName)
}
// merge flavors to get missing dimensions for variant
val missingDimensions = findMissingDimensions(androidExtension, variant)
val details = AndroidBuildVariant(variantName, buildTypeName, flavors, fallbacks, missingDimensions)
return AndroidVariantOrigin(tests, compilations, details)
}
private fun findMissingDimensions(androidExtension: DynamicBean, variant: DynamicBean): Map {
val missingDimensionsForVariant = mutableMapOf()
// default config has the lowest priority
missingDimensionsForVariant +=
androidExtension["defaultConfig"].value
© 2015 - 2025 Weber Informatics LLC | Privacy Policy