0
# Dependency Management
1
2
This document covers dependency resolution, automatic stdlib selection, and publishing configuration in Kotlin projects.
3
4
## Dependency Declaration
5
6
### Source Set Dependencies
7
8
```kotlin { .api }
9
interface KotlinDependencyHandler {
10
fun implementation(dependencyNotation: Any)
11
fun api(dependencyNotation: Any)
12
fun compileOnly(dependencyNotation: Any)
13
fun runtimeOnly(dependencyNotation: Any)
14
15
// Test dependencies
16
fun testImplementation(dependencyNotation: Any)
17
fun testCompileOnly(dependencyNotation: Any)
18
fun testRuntimeOnly(dependencyNotation: Any)
19
20
// Platform-specific configurations
21
fun expectedBy(dependency: Any)
22
}
23
```
24
25
**Usage Example:**
26
27
```kotlin
28
kotlin {
29
sourceSets {
30
commonMain {
31
dependencies {
32
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
33
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
34
}
35
}
36
37
commonTest {
38
dependencies {
39
implementation("org.jetbrains.kotlin:kotlin-test")
40
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
41
}
42
}
43
44
jvmMain {
45
dependencies {
46
implementation("ch.qos.logback:logback-classic:1.4.11")
47
implementation("io.ktor:ktor-server-netty:2.3.5")
48
}
49
}
50
51
jsMain {
52
dependencies {
53
implementation("org.jetbrains.kotlinx:kotlinx-html-js:0.9.1")
54
}
55
}
56
}
57
}
58
```
59
60
### Standard Library Management
61
62
The plugin automatically manages Kotlin standard library dependencies:
63
64
```kotlin { .api }
65
// Automatic stdlib selection based on targets
66
// JVM projects get kotlin-stdlib-jdk8
67
// JS projects get kotlin-stdlib-js
68
// Common projects get kotlin-stdlib-common
69
// Native projects get kotlin-stdlib automatically
70
```
71
72
**Stdlib Configuration:**
73
74
```kotlin
75
// Disable automatic stdlib dependency
76
kotlin {
77
explicitApi = org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode.Strict
78
79
// Custom stdlib version (not recommended)
80
// Use plugin version instead
81
}
82
83
// In dependencies block for manual control
84
dependencies {
85
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.2.0")
86
}
87
```
88
89
## NPM Dependencies (JavaScript)
90
91
### NPM Dependency Declaration
92
93
```kotlin { .api }
94
interface NpmDependency {
95
val name: String
96
val version: String
97
val scope: String?
98
}
99
100
interface NpmDependencyExtension {
101
fun npm(name: String, version: String): NpmDependency
102
fun npm(name: String, version: String, generateExternals: Boolean): NpmDependency
103
fun devNpm(name: String, version: String): NpmDependency
104
fun peerNpm(name: String, version: String): NpmDependency
105
fun optionalNpm(name: String, version: String): NpmDependency
106
}
107
```
108
109
**Usage Example:**
110
111
```kotlin
112
kotlin {
113
js(IR) {
114
browser()
115
nodejs()
116
}
117
118
sourceSets {
119
jsMain {
120
dependencies {
121
implementation("org.jetbrains.kotlin-wrappers:kotlin-react:18.2.0-pre.467")
122
123
// NPM dependencies
124
implementation(npm("lodash", "4.17.21"))
125
implementation(npm("axios", "1.5.0"))
126
implementation(devNpm("webpack", "5.88.0"))
127
}
128
}
129
}
130
}
131
```
132
133
### NPM Configuration
134
135
```kotlin { .api }
136
// In build.gradle.kts for JS projects
137
rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin> {
138
rootProject.the<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>().apply {
139
nodeVersion = "18.17.0"
140
npmVersion = "9.8.1"
141
}
142
}
143
```
144
145
## KAPT Dependencies
146
147
For annotation processing with KAPT:
148
149
```kotlin { .api }
150
// KAPT configuration
151
dependencies {
152
kapt("com.google.dagger:dagger-compiler:2.48")
153
kaptTest("com.google.dagger:dagger-compiler:2.48")
154
155
implementation("com.google.dagger:dagger:2.48")
156
}
157
158
kapt {
159
correctErrorTypes = true
160
useBuildCache = false
161
mapDiagnosticLocations = true
162
163
arguments {
164
arg("dagger.hilt.shareTestComponents", "true")
165
arg("room.schemaLocation", "$projectDir/schemas")
166
}
167
}
168
```
169
170
## KSP Dependencies
171
172
For Kotlin Symbol Processing:
173
174
```kotlin { .api }
175
dependencies {
176
ksp("com.google.devtools.ksp:symbol-processing:2.2.0-1.0.16")
177
kspTest("com.google.devtools.ksp:symbol-processing:2.2.0-1.0.16")
178
}
179
```
180
181
## Publishing Configuration
182
183
### Maven Publishing
184
185
```kotlin { .api }
186
publishing {
187
publications {
188
withType<MavenPublication> {
189
pom {
190
name.set("My Kotlin Library")
191
description.set("A Kotlin multiplatform library")
192
url.set("https://github.com/user/repo")
193
194
licenses {
195
license {
196
name.set("The Apache License, Version 2.0")
197
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
198
}
199
}
200
201
developers {
202
developer {
203
id.set("username")
204
name.set("User Name")
205
email.set("user@example.com")
206
}
207
}
208
209
scm {
210
connection.set("scm:git:git://github.com/user/repo.git")
211
developerConnection.set("scm:git:ssh://github.com/user/repo.git")
212
url.set("https://github.com/user/repo")
213
}
214
}
215
}
216
}
217
}
218
```
219
220
### Multiplatform Publishing
221
222
```kotlin { .api }
223
kotlin {
224
jvm {
225
mavenPublication {
226
artifactId = "mylib-jvm"
227
}
228
}
229
230
js(IR) {
231
mavenPublication {
232
artifactId = "mylib-js"
233
}
234
}
235
236
sourceSets {
237
commonMain {
238
dependencies {
239
// These will be included in all platform publications
240
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
241
}
242
}
243
}
244
}
245
```
246
247
### Android Library Publishing
248
249
```kotlin { .api }
250
kotlin {
251
android {
252
publishLibraryVariants("release", "debug")
253
// or
254
publishAllLibraryVariants()
255
}
256
}
257
```
258
259
## Dependency Configurations
260
261
### Configuration Types
262
263
```kotlin { .api }
264
// Main configurations
265
val implementation: Configuration
266
val api: Configuration
267
val compileOnly: Configuration
268
val runtimeOnly: Configuration
269
270
// Test configurations
271
val testImplementation: Configuration
272
val testCompileOnly: Configuration
273
val testRuntimeOnly: Configuration
274
275
// Platform-specific configurations (created automatically)
276
val jvmImplementation: Configuration
277
val jsImplementation: Configuration
278
val nativeImplementation: Configuration
279
```
280
281
### Custom Configurations
282
283
```kotlin
284
configurations {
285
val customImplementation by creating {
286
extendsFrom(implementation.get())
287
}
288
}
289
290
kotlin {
291
sourceSets {
292
commonMain {
293
dependencies {
294
"customImplementation"("some.group:artifact:1.0.0")
295
}
296
}
297
}
298
}
299
```
300
301
## Version Catalogs Integration
302
303
```kotlin { .api }
304
// With version catalogs (libs.versions.toml)
305
kotlin {
306
sourceSets {
307
commonMain {
308
dependencies {
309
implementation(libs.kotlinx.coroutines.core)
310
implementation(libs.kotlinx.serialization.json)
311
}
312
}
313
314
jvmMain {
315
dependencies {
316
implementation(libs.ktor.server.netty)
317
}
318
}
319
}
320
}
321
```
322
323
## Dependency Substitution
324
325
```kotlin { .api }
326
configurations.all {
327
resolutionStrategy {
328
dependencySubstitution {
329
substitute(module("org.jetbrains.kotlin:kotlin-stdlib-jdk7"))
330
.using(module("org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.2.0"))
331
.because("Use JDK 8 stdlib")
332
}
333
}
334
}
335
```
336
337
## Platform-Specific Dependencies
338
339
### JVM Dependencies
340
341
```kotlin
342
kotlin {
343
jvm {
344
compilations.all {
345
dependencies {
346
implementation("org.slf4j:slf4j-api:2.0.9")
347
implementation("ch.qos.logback:logback-classic:1.4.11")
348
349
// JVM-only libraries
350
implementation("com.fasterxml.jackson.core:jackson-core:2.15.2")
351
}
352
}
353
}
354
}
355
```
356
357
### Native Dependencies
358
359
```kotlin
360
kotlin {
361
linuxX64 {
362
compilations.main {
363
cinterops {
364
val libcurl by creating {
365
defFile(project.file("src/nativeInterop/cinterop/libcurl.def"))
366
packageName("libcurl")
367
}
368
}
369
}
370
}
371
}
372
```
373
374
## Transitive Dependencies
375
376
### Dependency Exclusions
377
378
```kotlin
379
kotlin {
380
sourceSets {
381
commonMain {
382
dependencies {
383
implementation("some.group:artifact:1.0.0") {
384
exclude(group = "unwanted.group", module = "unwanted-module")
385
}
386
}
387
}
388
}
389
}
390
```
391
392
### Force Dependency Versions
393
394
```kotlin
395
configurations.all {
396
resolutionStrategy {
397
force("org.jetbrains.kotlin:kotlin-stdlib:2.2.0")
398
failOnVersionConflict()
399
}
400
}
401
```