> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reasonos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin SDK

> Build Kotlin applications with RBS — auto-managed Kotlin compiler, JVM integration, Spring Boot, coroutines, and container images.

# Kotlin SDK

RBS provides first-class Kotlin support with automatic Kotlin compiler and JDK management, Maven-compatible dependency resolution, JUnit 5 auto-inclusion, Spring Boot support, and linting.

## Setup

### Toolchain

Register both Java and Kotlin toolchains — Kotlin requires a JVM.

```python filename="WORKSPACE.rbs" theme={null}
load("@rbs//java/toolchain.rbs", "java_toolchain", "java_test_deps")
load("@rbs//kotlin/toolchain.rbs", "kotlin_toolchain")

# JDK (Kotlin requires JVM)
java_toolchain(name = "java", version = "17.0.11")

# Kotlin compiler
kotlin_toolchain(name = "kotlin", version = "2.0.21")

# JUnit 5 for tests
java_test_deps()
```

### Dependencies

Define external packages using `kotlin_repository`. Uses the same Maven coordinate format as Java.

```python filename="WORKSPACE.rbs" theme={null}
load("@rbs//kotlin/dependencies.rbs", "kotlin_repository")

kotlin_repository(
    name = "spring_boot_web",
    package = "org.springframework.boot:spring-boot-starter-web",
    version = "3.2.2",
)

kotlin_repository(
    name = "kotlinx_coroutines",
    package = "org.jetbrains.kotlinx:kotlinx-coroutines-core",
    version = "1.8.1",
)
```

## Rules

### `kotlin_library`

Creates a reusable Kotlin library (JAR).

```python theme={null}
load("@rbs//kotlin/rules.rbs", "kotlin_library")

kotlin_library(
    name = "greeter",
    srcs = ["Greeter.kt"],
)
```

| Attribute      | Type   | Description                                      |
| -------------- | ------ | ------------------------------------------------ |
| `name`         | string | Unique target name (required).                   |
| `srcs`         | list   | Kotlin source files.                             |
| `deps`         | list   | Dependencies (local or `@external://`).          |
| `runtime_deps` | list   | Runtime-only dependencies (not for compilation). |
| `resources`    | list   | Resource files to include in the JAR.            |

### `kotlin_binary`

Creates an executable Kotlin application.

```python theme={null}
load("@rbs//kotlin/rules.rbs", "kotlin_binary")

kotlin_binary(
    name = "hello",
    srcs = ["Main.kt"],
    deps = [":greeter"],
    main = "MainKt",
)

# With external dependencies (coroutines)
kotlin_binary(
    name = "async_hello",
    srcs = ["AsyncMain.kt"],
    deps = [
        ":greeter",
        "@external://org_jetbrains_kotlinx_kotlinx-coroutines-core:1.8.1",
    ],
    main = "AsyncMainKt",
)
```

| Attribute      | Type   | Description                            |
| -------------- | ------ | -------------------------------------- |
| `name`         | string | Unique target name (required).         |
| `srcs`         | list   | Kotlin source files.                   |
| `main`         | string | Fully qualified main class (required). |
| `deps`         | list   | Dependencies.                          |
| `runtime_deps` | list   | Runtime-only dependencies.             |
| `resources`    | list   | Resource files.                        |
| `javaopts`     | list   | JVM options.                           |

### `kotlin_test`

Runs Kotlin tests. **JUnit 5 is automatically included.**

```python theme={null}
load("@rbs//kotlin/rules.rbs", "kotlin_test")

kotlin_test(
    name = "greeting_service_test",
    srcs = [
        "src/main/kotlin/com/example/GreetingService.kt",
        "src/test/kotlin/com/example/GreetingServiceTest.kt",
    ],
    args = ["--select-class=com.example.GreetingServiceTest"],
)
```

### `kotlin_lint` / `kt_lint`

Runs linting on Kotlin source files (supports ktlint).

```python theme={null}
load("@rbs//kotlin/lint.rbs", "kt_lint")

kt_lint(
    name = "lint",
    srcs = ["Main.kt", "Greeter.kt", "AsyncMain.kt"],
    ktlint = True,
)
```

## Example: Kotlin Spring Boot

<Tabs>
  <Tab title="WORKSPACE.rbs">
    ```python theme={null}
    load("@rbs//java/toolchain.rbs", "java_toolchain", "java_test_deps")
    load("@rbs//kotlin/toolchain.rbs", "kotlin_toolchain")
    load("@rbs//kotlin/dependencies.rbs", "kotlin_repository")

    java_toolchain(name = "java", version = "17.0.11")
    kotlin_toolchain(name = "kotlin", version = "2.0.21")
    java_test_deps()

    kotlin_repository(
        name = "spring_boot_web",
        package = "org.springframework.boot:spring-boot-starter-web",
        version = "3.2.2",
    )
    ```
  </Tab>

  <Tab title="BUILD.rbs">
    ```python theme={null}
    load("@rbs//kotlin/rules.rbs", "kotlin_binary", "kotlin_library", "kotlin_test", "kotlin_lint")
    load("@rbs//oci/rules.rbs", "oci_image")

    kotlin_library(
        name = "springboot_lib",
        srcs = glob(["src/main/kotlin/**/*.kt"]),
        deps = ["@external://org_springframework_boot_spring_boot_starter_web:3.2.2"],
    )

    kotlin_binary(
        name = "app",
        srcs = glob(["src/main/kotlin/**/*.kt"]),
        main = "com.example.springboot.ApplicationKt",
        resources = ["src/main/resources/application.properties"],
        deps = ["@external://org_springframework_boot_spring_boot_starter_web:3.2.2"],
        javaopts = ["-Djava.awt.headless=true"],
    )

    kotlin_test(
        name = "greeting_service_test",
        srcs = [
            "src/main/kotlin/com/example/springboot/GreetingService.kt",
            "src/test/kotlin/com/example/springboot/GreetingServiceTest.kt",
        ],
        deps = ["@external://org_springframework_boot_spring_boot_starter_web:3.2.2"],
        args = ["--select-class=com.example.springboot.GreetingServiceTest"],
    )

    kotlin_lint(
        name = "app_lint",
        srcs = glob(["src/main/kotlin/**/*.kt"]),
    )

    oci_image(
        name = "image",
        binary = ":app",
        base = "ubuntu:22.04",
        env = {"JAVA_OPTS": "-Djava.awt.headless=true"},
        tag = "kotlin-springboot-example:latest",
    )
    ```
  </Tab>

  <Tab title="Commands">
    ```bash theme={null}
    rbs build app                       # Build the application
    rbs run app                         # Run Spring Boot server
    rbs test greeting_service_test      # Run tests
    rbs build app_lint                  # Lint code
    rbs build image                     # Build container image
    ```
  </Tab>
</Tabs>
