Kotlin Multiplatform

This page contains examples of using Mill to build Kotlin Multiplatform projects, sharing code between platforms such as the JVM, Kotlin/JS and Android.

Kotlin Multiplatform support is experimental. It currently supports JVM, Kotlin/JS and Android platforms, and requires Kotlin 2.0 or later.

Hello Kotlin Multiplatform

This example shows a minimal Kotlin Multiplatform module foo, compiled for the JVM and for Kotlin/JS running on Node.js.

build.mill (download, browse)
package build
import mill.*, kotlinlib.*, kotlinlib.js.*, kotlinlib.multiplatform.*

object foo extends KotlinMultiplatformModule {
  def kotlinVersion = "2.2.20"

  object jvm extends PlatformModule {
    def mainClass = Some("foo.MainKt")

    object test extends PlatformTests, TestModule.Junit4 {
      def mvnDeps = Seq(mvn"org.jetbrains.kotlin:kotlin-test-junit:${this.kotlinVersion()}")
    }
  }

  object js extends PlatformModule, KotlinJsModule {
    def kotlinJsModuleKind = ModuleKind.ESModule
    def kotlinJsRunTarget = Some(RunTarget.Node)

    object test extends PlatformTests, KotlinTestPackageTests
  }
}

A KotlinMultiplatformModule contains one PlatformModule per target platform. Its sources are laid out by source set, as in the Kotlin Gradle plugin:

foo/src/
  commonMain/kotlin/      shared by all platforms
  commonMain/resources/   resources shared by all platforms
  jvmMain/kotlin/         only for `foo.jvm`
  jvmMain/java/           Java sources, only for `foo.jvm`
  jsMain/kotlin/          only for `foo.js`
  commonTest/kotlin/      tests shared by all platforms
  jvmTest/kotlin/         tests only for `foo.jvm.test`

Each platform module compiles its own source set together with commonMain, so expect declarations in commonMain can be implemented by actual declarations in jvmMain and jsMain. The source set used by a platform module is named after it, e.g. object jvm reads jvmMain, while the compiler is chosen by the traits it extends, e.g. KotlinJsModule for Kotlin/JS.

> ./mill foo.jvm.run
Hello from JVM

> ./mill foo.js.run
Hello from JS

> ./mill foo.jvm.test
...foo.GreetingTest...greetingMentionsPlatform...
...foo.JvmPlatformTest...platformIsJvm...

> ./mill foo.js.test

> cat out/foo/js/test/testForked.dest/test-report.xml
...name="Mocha Tests" tests="1" failures="0" errors="0"...

Renamed targets

The source sets of a platform module are named after it, while the compiler depends on its traits. So a JVM platform named desktop, like jvm("desktop") in the Kotlin Gradle plugin, compiles src/desktopMain and src/commonMain:

object desktop extends PlatformModule

To use source sets named differently from the module, override kmpPlatform:

object app extends PlatformModule {
  def kmpPlatform = "desktop" // compiles `src/desktopMain`, not `src/appMain`
}

Android

This example shows a Kotlin Multiplatform library shared, compiled for the JVM and for Android, used by a separate Android app androidApp.

build.mill (download, browse)
//| mill-jvm-version: zulu:21

package build
import mill.*, androidlib.*, androidlib.multiplatform.*, kotlinlib.*

object androidSdkModule0 extends AndroidSdkModule {
  def buildToolsVersion = "35.0.0"
}

trait AndroidSettings extends AndroidModule {
  def androidSdkModule = mill.api.ModuleRef(androidSdkModule0)
  def androidCompileSdk = 35
  def androidMinSdk = 24
}

object shared extends AndroidKotlinMultiplatformModule {
  def kotlinVersion = "2.2.20"

  trait SharedPlatformModule extends PlatformModule {
    def mvnDeps = super.mvnDeps() ++
      Seq(mvn"org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
    def kotlincPluginMvnDeps = super.kotlincPluginMvnDeps() ++ Seq(
      mvn"org.jetbrains.kotlin:kotlin-serialization-compiler-plugin:${this.kotlinVersion()}"
    )
  }

  object jvm extends SharedPlatformModule {
    object test extends PlatformTests, TestModule.Junit4 {
      def mvnDeps = Seq(mvn"org.jetbrains.kotlin:kotlin-test-junit:${this.kotlinVersion()}")
    }
  }

  object android extends AndroidPlatformModule, AndroidSettings, SharedPlatformModule {
    def androidNamespace = "com.example.shared"

    object test extends PlatformHostTests, TestModule.Junit4 {
      def mvnDeps = Seq(mvn"org.jetbrains.kotlin:kotlin-test-junit:${this.kotlinVersion()}")
    }
  }
}

object androidApp extends AndroidAppKotlinModule, AndroidSettings {
  def kotlinVersion = "2.2.20"
  def androidApplicationId = "com.example.app"
  def androidApplicationNamespace = "com.example.app"
  def moduleDeps = Seq(shared.android)
}

An AndroidKotlinMultiplatformModule can contain an AndroidPlatformModule, which compiles androidMain and commonMain against the Android SDK.

shared/src/
  commonMain/kotlin/        shared by all platforms
  jvmMain/kotlin/           only for `shared.jvm`
  androidMain/kotlin/       only for `shared.android`
  androidMain/res/          Android resources of `shared.android`
  commonTest/kotlin/        tests shared by all platforms
  jvmTest/kotlin/           tests only for `shared.jvm.test`
  androidHostTest/kotlin/   tests only for `shared.android.test`
androidApp/src/main/
  AndroidManifest.xml
  kotlin/
  res/

The AndroidManifest.xml of shared.android is optional, and generated from its androidNamespace unless src/androidMain/AndroidManifest.xml exists. Its PlatformHostTests run androidHostTest and commonTest on the JVM.

Dependencies of all platforms, like kotlinx-serialization-json here, go in a trait extended by every platform module, SharedPlatformModule.

> ./mill shared.jvm.test
...com.example.shared.GreetingTest...greetMentionsPlatform...
...com.example.shared.JvmPlatformTest...platformIsJava...
...com.example.shared.MessageTest...jsonRoundTrip...

> ./mill shared.android.test
...com.example.shared.AndroidPlatformTest...platformIsAndroid...
...com.example.shared.GreetingTest...greetMentionsPlatform...
...com.example.shared.MessageTest...jsonRoundTrip...

> ./mill show androidApp.androidApk
"...$MILL_OUT/androidApp/androidApk.dest/app.apk"