# Integrating with Kotlin Plugins

Kotlin standard libraries (stdlib) are added by Kotlin plugins by default via `implementation` (`runtimeClasspath`),
they will be bundled into the shadowed JARs automatically.
If you don't need a standard library at all, you can add the following Gradle property to your gradle.properties file:

```properties
kotlin.stdlib.default.dependency=false
```

Kotlin compilations may still require the standard libraries, you can add them into `compileOnly` (`compileClasspath`)
to make sure compilations success and avoid shadowing as follows:

=== "Kotlin"

    ```kotlin
    dependencies {
      compileOnly("org.jetbrains.kotlin:kotlin-stdlib:<version>")
    }
    ```

=== "Groovy"

    ```groovy
    dependencies {
      compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:<version>'
    }
    ```

See more information about
[Dependency on the standard library](https://kotlinlang.org/docs/gradle-configure-project.html#dependency-on-the-standard-library).

## For Kotlin JVM Plugin

Shadow works well for Kotlin JVM projects like Java projects. Here is an example:

=== "Kotlin"

    ```kotlin
    plugins {
      id("org.jetbrains.kotlin.jvm")
      id("com.gradleup.shadow")
    }

    dependencies {
      implementation("io.ktor:ktor-client-okhttp:3.1.0")
    }
    ```

=== "Groovy"

    ```groovy
    plugins {
      id 'org.jetbrains.kotlin.jvm'
      id 'com.gradleup.shadow'
    }

    dependencies {
      implementation 'io.ktor:ktor-client-okhttp:3.1.0'
    }
    ```

You can mix the Kotlin JVM plugin with `java-gradle-plugin`, `application`, and other Java plugins,
easily organize your build logic for [Publishing Libraries](../publishing/README.md),
[Running Applications](../application-plugin/README.md), and so on.

## For Kotlin Multiplatform Plugin

Shadow honors Kotlin's [`org.jetbrains.kotlin.multiplatform`][org.jetbrains.kotlin.multiplatform] plugin and will
automatically configure additional tasks for bundling the shadowed JAR for its `jvm` target.

=== "Kotlin"

    ```kotlin
    plugins {
      id("org.jetbrains.kotlin.multiplatform")
      id("com.gradleup.shadow")
    }

    kotlin {
      jvm().mainRun {
        // Optionally, set the main class for `runJvm`.
        mainClass = "myapp.MainKt"
      }
      sourceSets {
        commonMain {
          dependencies {
            implementation("io.ktor:ktor-client-core:<version>")
          }
        }
        jvmMain {
          dependencies {
            implementation("io.ktor:ktor-client-okhttp:<version>")
          }
        }
      }
    }

    // TODO: Gradle doesn't generate accessors for this use case, so we can't call `tasks.shadowJar` directly like the other examples.
    tasks.named<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("shadowJar") {
      manifest {
        // Optionally, set the main class for the shadowed JAR.
        attributes["Main-Class"] = "com.example.MainKt"
      }
    }
    ```

=== "Groovy"

    ```groovy
    plugins {
      id 'org.jetbrains.kotlin.multiplatform'
      id 'com.gradleup.shadow'
    }

    kotlin {
      jvm().mainRun {
        // Optionally, set the main class for `runJvm`.
        it.mainClass.set('myapp.MainKt')
      }
      sourceSets {
        commonMain {
          dependencies {
            implementation "io.ktor:ktor-client-core:<version>"
          }
        }
        jvmMain {
          dependencies {
            implementation "io.ktor:ktor-client-okhttp:<version>"
          }
        }
      }
    }

    tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
      manifest {
        // Optionally, set the main class for the shadowed JAR.
        attributes 'Main-Class': 'com.example.MainKt'
      }
    }
    ```

## Kotlin Module Metadata Remapping

Kotlin module metadata (`.kotlin_module`) files contain information about package parts and facades. When relocating
classes, Shadow automatically relocates the packages and facades inside the Kotlin module metadata files. This feature
is enabled by default via the deprecated `enableKotlinModuleRemapping` property.

To explicitly apply this remapping (recommended for future compatibility), add
[`KotlinModuleMetadataTransformer`][KotlinModuleMetadataTransformer] to your task configuration:

=== "Kotlin"

    ```kotlin
    tasks.shadowJar {
      transform(com.github.jengelman.gradle.plugins.shadow.transformers.KotlinModuleMetadataTransformer::class.java)
    }
    ```

=== "Groovy"

    ```groovy
    tasks.shadowJar {
      transform(com.github.jengelman.gradle.plugins.shadow.transformers.KotlinModuleMetadataTransformer)
    }
    ```

[org.jetbrains.kotlin.multiplatform]: https://kotlinlang.org/docs/multiplatform-intro.html
[KotlinModuleMetadataTransformer]: ../api/shadow/com.github.jengelman.gradle.plugins.shadow.transformers/-kotlin-module-metadata-transformer/index.html
