KT-49189: fix project(...) dependencies on MPP from pure-Java consumers

* Don't set the localToProject attribute anymore, as it prevents proper
  disambiguation if not set properly on all configurations, while all
  the intentionally-public configurations
  should already have the same value.

* Disambiguate o.j.k.platform.type Android vs JVM preferring JVM, as
  Android should have other attributes which make pure-Android consumers
  match the Android MPP variants.

* With Gradle 7.0+, set the attribute
  `org.gradle.api.attributes.java.TargetJvmEnvironment` on the JVM &
  Android elements configurations, which helps pure-Android consumers
  to match the Android, not JVM variants. This fixes KT-30961 for
  Gradle 7.0+ and AGP 7.0+

Issues: KT-49189, KT-30961
This commit is contained in:
Sergey Igushkin
2021-10-20 17:23:30 +04:00
committed by Space
parent f0e703e1ee
commit a474e8a00b
12 changed files with 179 additions and 58 deletions
@@ -29,14 +29,28 @@ enum class KotlinPlatformType: Named, Serializable {
class DisambiguationRule : AttributeDisambiguationRule<KotlinPlatformType> {
override fun execute(details: MultipleCandidatesDetails<KotlinPlatformType?>) = with(details) {
if (candidateValues == setOf(androidJvm, jvm))
closestMatch(androidJvm)
if (consumerValue in candidateValues) {
closestMatch(checkNotNull(consumerValue))
return@with
}
/**
* If the consumer doesn't request anything specific and matches both JVM and Android,
* then assume that it's an ordinary pure-Java consumer. If it's a pure-Android consumer, it will have
* other means of disambiguation that will take precedence
* (the buildType attribute, the target JVM environment = android)
*/
if (consumerValue == null && androidJvm in candidateValues && jvm in candidateValues) {
closestMatch(jvm)
return@with
}
if (common in candidateValues && jvm !in candidateValues && androidJvm !in candidateValues) {
// then the consumer requests common or requests no platform-specific artifacts,
// so common is the best match, KT-26834; apply this rule only when no JVM variant is available,
// as doing otherwise would conflict with Gradle java's disambiguation rules and lead to KT-32239
closestMatch(common)
return@with
}
}
}