0
# DSL Extensions
1
2
This document covers the Kotlin Gradle Plugin's Domain Specific Language (DSL) extensions for configuring Kotlin projects.
3
4
## Core Project Extension
5
6
### KotlinProjectExtension
7
8
The base extension available in all Kotlin projects.
9
10
```kotlin { .api }
11
interface KotlinProjectExtension {
12
val sourceSets: NamedDomainObjectContainer<KotlinSourceSet>
13
val compilerOptions: KotlinJvmCompilerOptions // or platform-specific options
14
15
fun sourceSets(configure: NamedDomainObjectContainer<KotlinSourceSet>.() -> Unit)
16
}
17
```
18
19
**Usage Example:**
20
21
```kotlin
22
kotlin {
23
compilerOptions {
24
jvmTarget.set(JvmTarget.JVM_11)
25
freeCompilerArgs.add("-Xjsr305=strict")
26
}
27
28
sourceSets {
29
main {
30
dependencies {
31
implementation("org.jetbrains.kotlin:kotlin-stdlib")
32
}
33
}
34
test {
35
dependencies {
36
implementation("org.jetbrains.kotlin:kotlin-test")
37
}
38
}
39
}
40
}
41
```
42
43
## Multiplatform Extension
44
45
### KotlinMultiplatformExtension
46
47
The main DSL extension for multiplatform projects.
48
49
```kotlin { .api }
50
interface KotlinMultiplatformExtension : KotlinProjectExtension {
51
// Target creation functions
52
fun jvm(name: String = "jvm", configure: KotlinJvmTarget.() -> Unit = {}): KotlinJvmTarget
53
fun js(name: String = "js", compiler: KotlinJsCompilerType = KotlinJsCompilerType.IR, configure: KotlinJsTarget.() -> Unit = {}): KotlinJsTarget
54
fun android(configure: KotlinAndroidTarget.() -> Unit = {}): KotlinAndroidTarget
55
56
// Native targets
57
fun iosX64(name: String = "iosX64", configure: KotlinNativeTarget.() -> Unit = {}): KotlinNativeTarget
58
fun iosArm64(name: String = "iosArm64", configure: KotlinNativeTarget.() -> Unit = {}): KotlinNativeTarget
59
fun macosX64(name: String = "macosX64", configure: KotlinNativeTarget.() -> Unit = {}): KotlinNativeTarget
60
fun linuxX64(name: String = "linuxX64", configure: KotlinNativeTarget.() -> Unit = {}): KotlinNativeTarget
61
fun mingwX64(name: String = "mingwX64", configure: KotlinNativeTarget.() -> Unit = {}): KotlinNativeTarget
62
63
// WASM targets
64
fun wasmJs(name: String = "wasmJs", configure: KotlinWasmJsTarget.() -> Unit = {}): KotlinWasmJsTarget
65
fun wasmWasi(name: String = "wasmWasi", configure: KotlinWasmWasiTarget.() -> Unit = {}): KotlinWasmWasiTarget
66
67
// Source set hierarchy
68
val sourceSets: KotlinSourceSetContainer
69
}
70
```
71
72
**Usage Example:**
73
74
```kotlin
75
kotlin {
76
// Configure targets
77
jvm {
78
jvmTarget = JvmTarget.JVM_11
79
}
80
81
js(IR) {
82
browser {
83
webpackTask {
84
outputFileName = "app.js"
85
}
86
}
87
nodejs()
88
}
89
90
iosX64()
91
iosArm64()
92
93
// Configure source sets
94
sourceSets {
95
commonMain {
96
dependencies {
97
implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
98
}
99
}
100
jvmMain {
101
dependencies {
102
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
103
}
104
}
105
jsMain {
106
dependencies {
107
implementation("org.jetbrains.kotlin:kotlin-stdlib-js")
108
}
109
}
110
nativeMain {
111
dependsOn(commonMain.get())
112
}
113
iosMain {
114
dependsOn(nativeMain.get())
115
}
116
}
117
}
118
```
119
120
## Compiler Options
121
122
### KotlinJvmCompilerOptions
123
124
JVM-specific compiler options.
125
126
```kotlin { .api }
127
interface KotlinJvmCompilerOptions : KotlinCommonCompilerOptions {
128
val jvmTarget: Property<JvmTarget>
129
val javaParameters: Property<Boolean>
130
val useIR: Property<Boolean>
131
}
132
```
133
134
### KotlinJsCompilerOptions
135
136
JavaScript-specific compiler options.
137
138
```kotlin { .api }
139
interface KotlinJsCompilerOptions : KotlinCommonCompilerOptions {
140
val sourceMap: Property<Boolean>
141
val sourceMapEmbedSources: Property<SourceMapEmbedSources>
142
val target: Property<String>
143
val moduleKind: Property<KotlinJsModuleKind>
144
}
145
```
146
147
### KotlinNativeCompilerOptions
148
149
Native-specific compiler options.
150
151
```kotlin { .api }
152
interface KotlinNativeCompilerOptions : KotlinCommonCompilerOptions {
153
val debuggable: Property<Boolean>
154
val optimized: Property<Boolean>
155
}
156
```
157
158
### KotlinCommonCompilerOptions
159
160
Common compiler options shared across all platforms.
161
162
```kotlin { .api }
163
interface KotlinCommonCompilerOptions {
164
val languageVersion: Property<KotlinVersion>
165
val apiVersion: Property<KotlinVersion>
166
val suppressWarnings: Property<Boolean>
167
val verbose: Property<Boolean>
168
val freeCompilerArgs: ListProperty<String>
169
val allWarningsAsErrors: Property<Boolean>
170
val progressiveMode: Property<Boolean>
171
}
172
```
173
174
**Usage Example:**
175
176
```kotlin
177
kotlin {
178
compilerOptions {
179
languageVersion.set(KotlinVersion.KOTLIN_2_0)
180
apiVersion.set(KotlinVersion.KOTLIN_2_0)
181
freeCompilerArgs.addAll(
182
"-Xjsr305=strict",
183
"-Xcontext-receivers"
184
)
185
allWarningsAsErrors.set(true)
186
}
187
}
188
```
189
190
## Source Set Configuration
191
192
### KotlinSourceSet
193
194
Configuration for individual source sets.
195
196
```kotlin { .api }
197
interface KotlinSourceSet {
198
val name: String
199
val kotlin: SourceDirectorySet
200
val resources: SourceDirectorySet
201
val dependencies: KotlinDependencyHandler
202
203
fun dependsOn(other: KotlinSourceSet)
204
fun dependencies(configure: KotlinDependencyHandler.() -> Unit)
205
}
206
```
207
208
### KotlinSourceSetContainer
209
210
Container for managing source sets.
211
212
```kotlin { .api }
213
interface KotlinSourceSetContainer : NamedDomainObjectContainer<KotlinSourceSet> {
214
val commonMain: NamedDomainObjectProvider<KotlinSourceSet>
215
val commonTest: NamedDomainObjectProvider<KotlinSourceSet>
216
217
// Platform-specific source sets created based on targets
218
fun getByName(name: String): KotlinSourceSet
219
fun create(name: String, configure: KotlinSourceSet.() -> Unit = {}): KotlinSourceSet
220
}
221
```
222
223
**Usage Example:**
224
225
```kotlin
226
kotlin {
227
sourceSets {
228
// Common source sets
229
commonMain {
230
dependencies {
231
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
232
}
233
}
234
235
commonTest {
236
dependencies {
237
implementation("org.jetbrains.kotlin:kotlin-test")
238
}
239
}
240
241
// Platform-specific source sets
242
jvmMain {
243
dependencies {
244
implementation("ch.qos.logback:logback-classic:1.4.11")
245
}
246
}
247
248
jsMain {
249
dependencies {
250
implementation("org.jetbrains.kotlinx:kotlinx-html-js:0.9.1")
251
}
252
}
253
254
// Custom source set hierarchy
255
val nativeMain by creating {
256
dependsOn(commonMain.get())
257
}
258
259
val iosMain by creating {
260
dependsOn(nativeMain)
261
}
262
}
263
}
264
```
265
266
## Extension Accessors
267
268
Helper functions to access platform-specific extensions.
269
270
```kotlin { .api }
271
// Extension accessors
272
fun Project.kotlinExtension(): KotlinProjectExtension
273
fun Project.multiplatformExtension(): KotlinMultiplatformExtension
274
fun Project.kotlinJvmExtension(): KotlinJvmProjectExtension
275
fun Project.kotlinAndroidExtension(): KotlinAndroidProjectExtension
276
```
277
278
## Container Interfaces
279
280
### KotlinTargetsContainer
281
282
Interface for managing multiple targets.
283
284
```kotlin { .api }
285
interface KotlinTargetsContainer {
286
fun <T : KotlinTarget> findByName(name: String): T?
287
fun <T : KotlinTarget> getByName(name: String): T
288
val targets: DomainObjectSet<KotlinTarget>
289
}
290
```
291
292
### KotlinTargetContainerWithPresetFunctions
293
294
Extended container with preset functions for common target types.
295
296
```kotlin { .api }
297
interface KotlinTargetContainerWithPresetFunctions : KotlinTargetsContainer {
298
fun jvm(): KotlinJvmTarget
299
fun android(): KotlinAndroidTarget
300
fun js(): KotlinJsTarget
301
fun linuxX64(): KotlinNativeTarget
302
fun macosX64(): KotlinNativeTarget
303
fun iosX64(): KotlinNativeTarget
304
}
305
```