CoursesKotlin Multiplatform, from the compiler up

Puzzle 21 · 22 of 32

One line, every target

Puzzle 20 showed that a klib is built for one target, yet in puzzle 19 you wrote one line for Ktor's core and every target's build got its own file. Here's the published file that makes that work, and how each compilation finds its part.

All puzzles in this course

The problem

Puzzle 20 opened Ktor's klib for iosArm64: its manifest names exactly one target. So Ktor must also publish a klib for iosSimulatorArm64, and something for Android. But in puzzle 19 you wrote one line:

commonMain.dependencies {
    implementation("io.ktor:ktor-client-core:3.6.1")
}

That name has no target in it. How does the iosArm64 compilation end up with the iosArm64 klib, the simulator compilation with the simulator one, and the Android build with something it can use? And why did the OkHttp part from puzzle 19 fail on iOS instead of quietly giving iOS nothing?

See it

Your one line names Ktor core's root publication; its .module file lists variants tagged ios_arm64, ios_simulator_arm64, jvm and common, each pointing at its own publication; the iosArm64 and simulator compilations each get the matching variant, and Android's androidJvm request is given the jvm variant; OkHttp's .module file has no ios_arm64 variant, so the iOS compilation fails ktor-client-core root publication .module ios_arm64 ios_simulator_arm64 jvm common iosarm64 klib simulator klib jvm jar iosArm64simulatorAndroid androidJvm → jvm ktor-client-okhttp .module jvm common no match

Scroll sideways to see the full diagram.

  1. Your one line names Ktor's core without any target: its root publication. The other publications each hold one target's files.

    Step 1 of 5
  2. The root holds a .module file in Gradle module metadata: a list of variants, each tagged with the target it's built for.

    Step 2 of 5
  3. Each variant points at the publication holding its files. Predict: which file does iosArm64's compilation get?

    Step 3 of 5
  4. The iosArm64 klib. Each compilation asks for the variant matching its target. Android asks for androidJvm; there's none, and Kotlin's plugin accepts the jvm one instead.

    Step 4 of 5
  5. The OkHttp part's .module file has only jvm and common. The iosArm64 compilation finds no match, so the build stops with an error.

    Step 5 of 5

The building block

When a multiplatform library is published, the multiplatform plugin creates one publication per target: a separately named package of files on the library server, such as Maven Central. Ktor's core has, among others:

PublicationWhat it holds
ktor-client-core-iosarm64the iosArm64 klib from puzzle 20
ktor-client-core-iossimulatorarm64the iosSimulatorArm64 klib
ktor-client-core-jvmclass files in a jar, the JVM output from puzzle 0
ktor-client-corethe root publication: no target of its own

Your line names the root, the one without a target. The root publication holds a small file, ktor-client-core-3.6.1.module, written in Gradle module metadata: a format, published next to a library, that tells Gradle what the library offers. Its main part is a list of variants. A variant is one version of the library, built for one kind of consumer and tagged with attributes: labels such as which platform and which native target it's for. Here is one entry from the real file, shortened:

{
  "name": "iosArm64ApiElements-published",
  "attributes": {
    "org.jetbrains.kotlin.platform.type": "native",
    "org.jetbrains.kotlin.native.target": "ios_arm64"
  },
  "available-at": {
    "module": "ktor-client-core-iosarm64"
  }
}

So the root doesn't contain the klibs. It points at them: "the variant for ios_arm64 is available at that publication".

Two words here have other meanings you already know, so keep them apart. A variant in this file is not an Android build variant (debug or release, or a product flavor, chosen inside your own app's build): it's one of the library's published builds, chosen by target. And "module" now has three meanings: a Gradle module of your project, such as :shared, written with a colon in a build file; Ktor's published library, which Gradle also calls a module; and the .module file, the metadata that describes it.

Now follow your one line through puzzle 16's compilations. Each compilation asks Gradle for the library, and says what it is: "I'm native, ios_arm64". Gradle reads the .module file and picks the variant whose attributes match:

  • iosArm64's compilation gets the ios_arm64 variant: Ktor's iosArm64 klib.
  • iosSimulatorArm64's compilation gets the ios_simulator_arm64 variant: the other klib.
  • The Android build asks for platform type androidJvm. Ktor's core publishes no androidJvm variant, only a jvm one, and Kotlin's plugin accepts a jvm variant for an androidJvm request: an Android build starts from JVM class files anyway, converting them to DEX, as puzzle 0 showed.
  • While Kotlin checks commonMain's own code, it gets a variant marked common: the declarations every target shares, so common code can only call what every target has.

Now the OkHttp failure from puzzle 19 is exact. The OkHttp part's .module file lists only a common and a jvm variant. When the iosArm64 compilation asks for a variant matching ios_arm64, there's none, so Gradle stops the build with an error that no variant matches. It never silently gives iOS nothing.

One more consequence. If a library forgets to publish one target, only the compilation for that target fails: the list in its .module file is the library's real promise, not its name.

The analogy

A wholesaler's catalogue. You order "Ktor core" by its catalogue name. The catalogue page lists one edition per country, each labelled with the country and the plug type, and says which warehouse shelf it's on. Each office's order is filled with the edition whose labels match it. If no edition matches an office's labels, that office's order is refused, not filled with nothing.

Check yourself

A library's .module file lists variants for jvm, ios_arm64 and common, but not ios_simulator_arm64. You add it to commonMain in a project with android, iosArm64 and iosSimulatorArm64 targets. What happens?

Not quite. A klib is built for one target; the simulator's compilation needs a variant tagged with its own target.

Right. Each compilation needs its own matching variant.

Not quite. The iosArm64 compilation finds its matching variant too; only the simulator has none.

Why can one dependency line in commonMain serve all three targets?

Not quite. Nothing is rebuilt: each target's files were already published, and Gradle picks among them.

Not quite. Each target has its own klib or jar; the root only lists them and says where they are.

Right. The root publication is a list of variants.

Puzzle 20's klib manifest named ios_arm64. Where else is that same target written, so that Gradle can pick the klib before opening it?

Not quite. Your line names only the root; the target is written in the published metadata. Re-read that part ↑

Right. Variants carry their target as attributes.

Not quite. The variant's attributes let Gradle choose without opening any klib.

Sources
  • Kotlin docs: Setting up multiplatform library publication ("Publications of a Kotlin Multiplatform library include multiple Maven publications, each corresponding to a specific target. Additionally, an umbrella root publication, kotlinMultiplatform, that represents the entire library is published. When added as a dependency to the common source set, the root publication automatically resolves to the appropriate platform-specific artifacts."; "The root publication serves as an entry point that references all target-specific publications"; "This task publishes a *.module file with information about the target-specific publications")
  • Gradle docs: Gradle Module Metadata ("Gradle Module Metadata is a unique format aimed at improving dependency resolution by making it multi-platform and variant-aware"; "consistent selection of platform-specific dependencies"; "Each variant must have at least one attribute")
  • Maven Central: ktor-client-core 3.6.1 .module file, read for this puzzle: variant iosArm64ApiElements-published with attributes "org.jetbrains.kotlin.platform.type": "native" and "org.jetbrains.kotlin.native.target": "ios_arm64", available-at module ktor-client-core-iosarm64; a metadataApiElements variant with "org.jetbrains.kotlin.platform.type": "common"; jvmApiElements-published with platform type jvm; no variant with platform type androidJvm
  • Maven Central: ktor-client-okhttp 3.6.1 .module file: variants only for common and jvm
  • Ktor docs: Client engines (its table of supported platforms has the row Engine "OkHttp", Platforms "JVM, Android")
  • Kotlin Gradle plugin: KotlinPlatformType.kt, Kotlin 2.4.21 (the compatibility rule for the platform-type attribute: if (producerValue == jvm && consumerValue == androidJvm) then compatible())

What you now know

A multiplatform library is published as one publication per target, plus a root publication. The root's Gradle module metadata file lists every variant of the library, each tagged with attributes such as the platform and the native target, and points at where that variant's files are. Each compilation asks for the variant whose attributes match its own target, so one dependency line resolves to the right klib or jar for every target.