Allow the common platform type consumers to consume platform artifacts

When a Kotlin metadata input configuration tries to resolve a module
with no Kotlin metadata (such as: plain old Maven module, a non-MPP
Kotlin project with a single platform), it should be able to resolve
successfully.

Given that the input metadata configurations extend from source set
dependencies configurations, where platform-specific dependencies may
occur, allowing the metadata configuration to resolve such dependencies
into platform artifacts seems to achieve successful dependency
resolution.

Issue #KT-26834 Fixed
This commit is contained in:
Sergey Igushkin
2018-09-21 22:29:18 +03:00
parent 269410d7aa
commit 76b51b1058
2 changed files with 36 additions and 15 deletions
@@ -6,9 +6,8 @@
package org.jetbrains.kotlin.gradle.plugin
import org.gradle.api.Named
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeCompatibilityRule
import org.gradle.api.attributes.CompatibilityCheckDetails
import org.gradle.api.attributes.*
import org.gradle.util.GradleVersion
import java.io.Serializable
enum class KotlinPlatformType: Named, Serializable {
@@ -17,19 +16,45 @@ enum class KotlinPlatformType: Named, Serializable {
override fun toString(): String = name
override fun getName(): String = name
class CompatibilityRule : AttributeCompatibilityRule<KotlinPlatformType> {
override fun execute(details: CompatibilityCheckDetails<KotlinPlatformType>) = with(details) {
if (producerValue == jvm && consumerValue == androidJvm)
compatible()
// Allow the input metadata configuration consume platform-specific artifacts if no metadata is available, KT-26834
if (consumerValue == common)
compatible()
}
}
class DisambiguationRule : AttributeDisambiguationRule<KotlinPlatformType> {
override fun execute(details: MultipleCandidatesDetails<KotlinPlatformType?>) = with(details) {
if (candidateValues == setOf(androidJvm, jvm))
closestMatch(androidJvm)
if (common in candidateValues)
// then the consumer requests common or requests no platform-specific artifacts,
// so common is the best match, KT-26834
closestMatch(common)
}
}
companion object {
val attribute = Attribute.of(
"org.jetbrains.kotlin.platform.type",
KotlinPlatformType::class.java
)
}
class CompatibilityRule : AttributeCompatibilityRule<KotlinPlatformType> {
override fun execute(details: CompatibilityCheckDetails<KotlinPlatformType>) = with(details) {
when {
producerValue == jvm && consumerValue == androidJvm -> compatible()
producerValue == consumerValue -> compatible()
fun setupAttributesMatchingStrategy(attributesSchema: AttributesSchema) {
attributesSchema.attribute(KotlinPlatformType.attribute).run {
if (isGradleVersionAtLeast(4, 0)) {
compatibilityRules.add(CompatibilityRule::class.java)
disambiguationRules.add(DisambiguationRule::class.java)
}
}
}
}
}
}
private fun isGradleVersionAtLeast(major: Int, minor: Int) =
GradleVersion.current() >= GradleVersion.version("$major.$minor")
@@ -78,11 +78,7 @@ abstract class KotlinBasePluginWrapper(
}
private fun setupAttributeMatchingStrategy(project: Project) = with(project.dependencies.attributesSchema) {
attribute(KotlinPlatformType.attribute).run {
if (isGradleVersionAtLeast(4, 0)) {
compatibilityRules.add(KotlinPlatformType.CompatibilityRule::class.java)
}
}
KotlinPlatformType.setupAttributesMatchingStrategy(this)
}
internal abstract fun getPlugin(