0
# Multiplatform Targets
1
2
This document covers the configuration and management of multiple platform targets in Kotlin Multiplatform projects.
3
4
## Target Types
5
6
### JVM Target
7
8
```kotlin { .api }
9
interface KotlinJvmTarget : KotlinTarget {
10
var withJava: Boolean
11
fun withJava()
12
13
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
14
15
// Compilation configuration
16
val compilations: NamedDomainObjectContainer<KotlinJvmCompilation>
17
}
18
```
19
20
**Usage Example:**
21
22
```kotlin
23
kotlin {
24
jvm {
25
withJava() // Enable Java support
26
27
compilations.all {
28
kotlinOptions {
29
jvmTarget = "11"
30
freeCompilerArgs += "-Xjsr305=strict"
31
}
32
}
33
34
testRuns["test"].executionTask.configure {
35
useJUnitPlatform()
36
}
37
}
38
}
39
```
40
41
### JavaScript Target
42
43
```kotlin { .api }
44
interface KotlinJsTarget : KotlinTarget {
45
val irTarget: Boolean
46
47
fun browser(configure: KotlinJsBrowserDsl.() -> Unit = {})
48
fun nodejs(configure: KotlinJsNodeDsl.() -> Unit = {})
49
50
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
51
}
52
53
interface KotlinJsBrowserDsl {
54
fun webpackTask(configure: KotlinWebpack.() -> Unit)
55
fun runTask(configure: KotlinWebpack.() -> Unit)
56
fun testTask(configure: KotlinWebpack.() -> Unit)
57
58
val distribution: Distribution
59
}
60
61
interface KotlinJsNodeDsl {
62
fun runTask(configure: NodeJsExec.() -> Unit)
63
fun testTask(configure: NodeJsExec.() -> Unit)
64
}
65
```
66
67
**Usage Example:**
68
69
```kotlin
70
kotlin {
71
js(IR) {
72
browser {
73
webpackTask {
74
outputFileName = "app.js"
75
sourceMaps = true
76
}
77
78
distribution {
79
outputDirectory = File("$projectDir/web")
80
}
81
}
82
83
nodejs {
84
runTask {
85
args.add("--experimental-modules")
86
}
87
}
88
}
89
}
90
```
91
92
### Native Targets
93
94
```kotlin { .api }
95
interface KotlinNativeTarget : KotlinTarget {
96
val konanTarget: KonanTarget
97
98
fun binaries(configure: KotlinNativeBinaryContainer.() -> Unit)
99
100
val binaries: KotlinNativeBinaryContainer
101
val testRuns: NamedDomainObjectContainer<KotlinTargetTestRun<*>>
102
}
103
104
interface KotlinNativeBinaryContainer {
105
fun executable(configure: KotlinNativeExecutable.() -> Unit = {})
106
fun staticLib(configure: KotlinNativeStaticLibrary.() -> Unit = {})
107
fun sharedLib(configure: KotlinNativeSharedLibrary.() -> Unit = {})
108
fun framework(configure: KotlinNativeFramework.() -> Unit = {})
109
}
110
```
111
112
**Native Target Creation Functions:**
113
114
```kotlin { .api }
115
// Apple platforms
116
fun iosX64(): KotlinNativeTarget
117
fun iosArm64(): KotlinNativeTarget
118
fun iosSimulatorArm64(): KotlinNativeTarget
119
fun macosX64(): KotlinNativeTarget
120
fun macosArm64(): KotlinNativeTarget
121
fun watchosX64(): KotlinNativeTarget
122
fun watchosArm32(): KotlinNativeTarget
123
fun watchosArm64(): KotlinNativeTarget
124
fun tvosX64(): KotlinNativeTarget
125
fun tvosArm64(): KotlinNativeTarget
126
127
// Linux platforms
128
fun linuxX64(): KotlinNativeTarget
129
fun linuxArm64(): KotlinNativeTarget
130
131
// Windows platforms
132
fun mingwX64(): KotlinNativeTarget
133
134
// Android Native
135
fun androidNativeArm32(): KotlinNativeTarget
136
fun androidNativeArm64(): KotlinNativeTarget
137
fun androidNativeX86(): KotlinNativeTarget
138
fun androidNativeX64(): KotlinNativeTarget
139
```
140
141
**Usage Example:**
142
143
```kotlin
144
kotlin {
145
iosX64 {
146
binaries {
147
framework {
148
baseName = "SharedFramework"
149
isStatic = true
150
}
151
}
152
}
153
154
iosArm64 {
155
binaries {
156
framework {
157
baseName = "SharedFramework"
158
isStatic = true
159
}
160
}
161
}
162
163
linuxX64 {
164
binaries {
165
executable {
166
entryPoint = "com.example.main"
167
}
168
}
169
}
170
}
171
```
172
173
### WebAssembly Targets
174
175
```kotlin { .api }
176
interface KotlinWasmJsTarget : KotlinTarget {
177
fun browser(configure: KotlinWasmJsBrowserDsl.() -> Unit = {})
178
fun nodejs(configure: KotlinWasmJsNodeDsl.() -> Unit = {})
179
fun d8(configure: KotlinWasmJsD8Dsl.() -> Unit = {})
180
}
181
182
interface KotlinWasmWasiTarget : KotlinTarget {
183
fun nodejs(configure: KotlinWasmWasiNodeDsl.() -> Unit = {})
184
}
185
```
186
187
**Usage Example:**
188
189
```kotlin
190
kotlin {
191
wasmJs {
192
browser {
193
webpackTask {
194
outputFileName = "app.wasm.js"
195
}
196
}
197
nodejs()
198
}
199
200
wasmWasi {
201
nodejs()
202
}
203
}
204
```
205
206
### Android Target
207
208
```kotlin { .api }
209
interface KotlinAndroidTarget : KotlinTarget {
210
val publishLibraryVariants: ListProperty<String>
211
val publishAllLibraryVariants: Property<Boolean>
212
213
fun publishLibraryVariants(vararg variants: String)
214
fun publishAllLibraryVariants()
215
}
216
```
217
218
**Usage Example:**
219
220
```kotlin
221
kotlin {
222
android {
223
publishLibraryVariants("release", "debug")
224
}
225
}
226
```
227
228
## Target Configuration
229
230
### Base Target Interface
231
232
```kotlin { .api }
233
interface KotlinTarget {
234
val name: String
235
val platformType: KotlinPlatformType
236
val targetName: String
237
238
val compilations: NamedDomainObjectContainer<out KotlinCompilation<*>>
239
val attributes: AttributeContainer
240
241
fun compilations(configure: NamedDomainObjectContainer<out KotlinCompilation<*>>.() -> Unit)
242
}
243
```
244
245
### Target Hierarchies
246
247
Setting up shared source sets between targets:
248
249
```kotlin
250
kotlin {
251
// Define targets
252
jvm()
253
js(IR) { browser(); nodejs() }
254
iosX64()
255
iosArm64()
256
257
sourceSets {
258
commonMain {
259
dependencies {
260
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
261
}
262
}
263
264
// Intermediate source sets
265
val nativeMain by creating {
266
dependsOn(commonMain.get())
267
}
268
269
val iosMain by creating {
270
dependsOn(nativeMain)
271
}
272
273
// Target-specific source sets
274
iosX64Main {
275
dependsOn(iosMain)
276
}
277
278
iosArm64Main {
279
dependsOn(iosMain)
280
}
281
}
282
}
283
```
284
285
## Binary Configuration
286
287
### Native Binaries
288
289
```kotlin { .api }
290
interface KotlinNativeBinary {
291
val name: String
292
val outputFile: File
293
val linkTask: KotlinNativeLink
294
val runTask: Task?
295
296
val baseName: String
297
val debuggable: Boolean
298
val optimized: Boolean
299
}
300
301
interface KotlinNativeExecutable : KotlinNativeBinary {
302
val entryPoint: String?
303
}
304
305
interface KotlinNativeLibrary : KotlinNativeBinary
306
interface KotlinNativeStaticLibrary : KotlinNativeLibrary
307
interface KotlinNativeSharedLibrary : KotlinNativeLibrary
308
309
interface KotlinNativeFramework : KotlinNativeBinary {
310
val isStatic: Boolean
311
val embedBitcode: FrameworkEmbedBitcodeMode
312
val transitiveExport: Boolean
313
}
314
```
315
316
**Binary Configuration Example:**
317
318
```kotlin
319
kotlin {
320
iosX64 {
321
binaries {
322
framework {
323
baseName = "MyFramework"
324
isStatic = true
325
embedBitcode = org.jetbrains.kotlin.gradle.plugin.mpp.FrameworkEmbedBitcodeMode.BITCODE
326
327
export(project(":shared"))
328
transitiveExport = true
329
}
330
331
executable {
332
entryPoint = "com.example.main"
333
debuggable = true
334
}
335
}
336
}
337
}
338
```
339
340
## Compilation Configuration
341
342
### Target Compilations
343
344
```kotlin { .api }
345
interface KotlinCompilation<T : KotlinCommonCompilerOptions> {
346
val target: KotlinTarget
347
val compilationName: String
348
val kotlinSourceSets: Set<KotlinSourceSet>
349
350
val compilerOptions: HasCompilerOptions<T>
351
val compileKotlinTask: KotlinCompilationTask<T>
352
353
val output: KotlinCompilationOutput
354
val dependencies: KotlinDependencyHandler
355
356
fun dependencies(configure: KotlinDependencyHandler.() -> Unit)
357
}
358
```
359
360
**Compilation Configuration Example:**
361
362
```kotlin
363
kotlin {
364
targets.all {
365
compilations.all {
366
compilerOptions.configure {
367
allWarningsAsErrors.set(true)
368
freeCompilerArgs.add("-Xopt-in=kotlin.RequiresOptIn")
369
}
370
}
371
}
372
373
// Per-target compilation configuration
374
jvm {
375
compilations.all {
376
compilerOptions.configure {
377
jvmTarget.set(JvmTarget.JVM_11)
378
}
379
}
380
}
381
}
382
```
383
384
## Publishing Configuration
385
386
### Target Publishing
387
388
```kotlin { .api }
389
interface KotlinTargetPublishing {
390
val publishJavadoc: Property<Boolean>
391
val publishSources: Property<Boolean>
392
}
393
```
394
395
**Publishing Configuration Example:**
396
397
```kotlin
398
kotlin {
399
jvm {
400
// Configure Maven publishing
401
mavenPublication {
402
artifactId = "mylib-jvm"
403
}
404
}
405
406
js(IR) {
407
mavenPublication {
408
artifactId = "mylib-js"
409
}
410
}
411
}
412
```
413
414
## Platform Types
415
416
```kotlin { .api }
417
enum class KotlinPlatformType {
418
androidJvm,
419
jvm,
420
js,
421
wasm,
422
native,
423
common
424
}
425
```
426
427
## Target Presets (Legacy)
428
429
While target presets are mostly replaced by direct target creation functions, they're still available:
430
431
```kotlin { .api }
432
interface KotlinTargetPreset<T : KotlinTarget> {
433
val name: String
434
fun createTarget(name: String): T
435
}
436
```