Official Gradle plugin for the Kotlin programming language that provides comprehensive build support for Kotlin projects including compilation, multiplatform development, Android integration, and native development capabilities
—
This document covers the configuration and management of multiple platform targets in Kotlin Multiplatform projects.
interface KotlinJvmTarget : KotlinTarget {
var withJava: Boolean
fun withJava()
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
// Compilation configuration
val compilations: NamedDomainObjectContainer<KotlinJvmCompilation>
}Usage Example:
kotlin {
jvm {
withJava() // Enable Java support
compilations.all {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs += "-Xjsr305=strict"
}
}
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
}interface KotlinJsTarget : KotlinTarget {
val irTarget: Boolean
fun browser(configure: KotlinJsBrowserDsl.() -> Unit = {})
fun nodejs(configure: KotlinJsNodeDsl.() -> Unit = {})
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
}
interface KotlinJsBrowserDsl {
fun webpackTask(configure: KotlinWebpack.() -> Unit)
fun runTask(configure: KotlinWebpack.() -> Unit)
fun testTask(configure: KotlinWebpack.() -> Unit)
val distribution: Distribution
}
interface KotlinJsNodeDsl {
fun runTask(configure: NodeJsExec.() -> Unit)
fun testTask(configure: NodeJsExec.() -> Unit)
}Usage Example:
kotlin {
js(IR) {
browser {
webpackTask {
outputFileName = "app.js"
sourceMaps = true
}
distribution {
outputDirectory = File("$projectDir/web")
}
}
nodejs {
runTask {
args.add("--experimental-modules")
}
}
}
}interface KotlinNativeTarget : KotlinTarget {
val konanTarget: KonanTarget
fun binaries(configure: KotlinNativeBinaryContainer.() -> Unit)
val binaries: KotlinNativeBinaryContainer
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
}
interface KotlinNativeBinaryContainer {
fun executable(configure: KotlinNativeExecutable.() -> Unit = {})
fun staticLib(configure: KotlinNativeStaticLibrary.() -> Unit = {})
fun sharedLib(configure: KotlinNativeSharedLibrary.() -> Unit = {})
fun framework(configure: KotlinNativeFramework.() -> Unit = {})
}Native Target Creation Functions:
// Apple platforms
fun iosX64(): KotlinNativeTarget
fun iosArm64(): KotlinNativeTarget
fun iosSimulatorArm64(): KotlinNativeTarget
fun macosX64(): KotlinNativeTarget
fun macosArm64(): KotlinNativeTarget
fun watchosX64(): KotlinNativeTarget
fun watchosArm32(): KotlinNativeTarget
fun watchosArm64(): KotlinNativeTarget
fun tvosX64(): KotlinNativeTarget
fun tvosArm64(): KotlinNativeTarget
// Linux platforms
fun linuxX64(): KotlinNativeTarget
fun linuxArm64(): KotlinNativeTarget
// Windows platforms
fun mingwX64(): KotlinNativeTarget
// Android Native
fun androidNativeArm32(): KotlinNativeTarget
fun androidNativeArm64(): KotlinNativeTarget
fun androidNativeX86(): KotlinNativeTarget
fun androidNativeX64(): KotlinNativeTargetUsage Example:
kotlin {
iosX64 {
binaries {
framework {
baseName = "SharedFramework"
isStatic = true
}
}
}
iosArm64 {
binaries {
framework {
baseName = "SharedFramework"
isStatic = true
}
}
}
linuxX64 {
binaries {
executable {
entryPoint = "com.example.main"
}
}
}
}interface KotlinWasmJsTarget : KotlinTarget {
fun browser(configure: KotlinWasmJsBrowserDsl.() -> Unit = {})
fun nodejs(configure: KotlinWasmJsNodeDsl.() -> Unit = {})
fun d8(configure: KotlinWasmJsD8Dsl.() -> Unit = {})
}
interface KotlinWasmWasiTarget : KotlinTarget {
fun nodejs(configure: KotlinWasmWasiNodeDsl.() -> Unit = {})
}Usage Example:
kotlin {
wasmJs {
browser {
webpackTask {
outputFileName = "app.wasm.js"
}
}
nodejs()
}
wasmWasi {
nodejs()
}
}interface KotlinAndroidTarget : KotlinTarget {
val publishLibraryVariants: ListProperty<String>
val publishAllLibraryVariants: Property<Boolean>
fun publishLibraryVariants(vararg variants: String)
fun publishAllLibraryVariants()
}Usage Example:
kotlin {
android {
publishLibraryVariants("release", "debug")
}
}interface KotlinTarget {
val name: String
val platformType: KotlinPlatformType
val targetName: String
val compilations: NamedDomainObjectContainer<out KotlinCompilation<*>>
val attributes: AttributeContainer
fun compilations(configure: NamedDomainObjectContainer<out KotlinCompilation<*>>.() -> Unit)
}Setting up shared source sets between targets:
kotlin {
// Define targets
jvm()
js(IR) { browser(); nodejs() }
iosX64()
iosArm64()
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}
}
// Intermediate source sets
val nativeMain by creating {
dependsOn(commonMain.get())
}
val iosMain by creating {
dependsOn(nativeMain)
}
// Target-specific source sets
iosX64Main {
dependsOn(iosMain)
}
iosArm64Main {
dependsOn(iosMain)
}
}
}interface KotlinNativeBinary {
val name: String
val outputFile: File
val linkTask: KotlinNativeLink
val runTask: Task?
val baseName: String
val debuggable: Boolean
val optimized: Boolean
}
interface KotlinNativeExecutable : KotlinNativeBinary {
val entryPoint: String?
}
interface KotlinNativeLibrary : KotlinNativeBinary
interface KotlinNativeStaticLibrary : KotlinNativeLibrary
interface KotlinNativeSharedLibrary : KotlinNativeLibrary
interface KotlinNativeFramework : KotlinNativeBinary {
val isStatic: Boolean
val embedBitcode: FrameworkEmbedBitcodeMode
val transitiveExport: Boolean
}Binary Configuration Example:
kotlin {
iosX64 {
binaries {
framework {
baseName = "MyFramework"
isStatic = true
embedBitcode = org.jetbrains.kotlin.gradle.plugin.mpp.FrameworkEmbedBitcodeMode.BITCODE
export(project(":shared"))
transitiveExport = true
}
executable {
entryPoint = "com.example.main"
debuggable = true
}
}
}
}interface KotlinCompilation<T : KotlinCommonCompilerOptions> {
val target: KotlinTarget
val compilationName: String
val kotlinSourceSets: Set<KotlinSourceSet>
val compilerOptions: HasCompilerOptions<T>
val compileKotlinTask: KotlinCompilationTask<T>
val output: KotlinCompilationOutput
val dependencies: KotlinDependencyHandler
fun dependencies(configure: KotlinDependencyHandler.() -> Unit)
}Compilation Configuration Example:
kotlin {
targets.all {
compilations.all {
compilerOptions.configure {
allWarningsAsErrors.set(true)
freeCompilerArgs.add("-Xopt-in=kotlin.RequiresOptIn")
}
}
}
// Per-target compilation configuration
jvm {
compilations.all {
compilerOptions.configure {
jvmTarget.set(JvmTarget.JVM_11)
}
}
}
}interface KotlinTargetPublishing {
val publishJavadoc: Property<Boolean>
val publishSources: Property<Boolean>
}Publishing Configuration Example:
kotlin {
jvm {
// Configure Maven publishing
mavenPublication {
artifactId = "mylib-jvm"
}
}
js(IR) {
mavenPublication {
artifactId = "mylib-js"
}
}
}enum class KotlinPlatformType {
androidJvm,
jvm,
js,
wasm,
native,
common
}While target presets are mostly replaced by direct target creation functions, they're still available:
interface KotlinTargetPreset<T : KotlinTarget> {
val name: String
fun createTarget(name: String): T
}Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-gradle-plugin