kotlin-dsl

Kotlin DSL - type-safe builders, Gradle DSL, @DslMarker

⭐5stars🔀1forks📁View on GitHub🕐Updated Jan 11, 2026

When & Why to Use This Skill

This Claude skill empowers developers to master Kotlin DSL (Domain-Specific Language) development, focusing on creating type-safe builders and sophisticated Gradle configurations. By leveraging core Kotlin features like @DslMarker, it helps eliminate scope pollution and ensures compile-time safety, significantly improving code readability and maintainability in complex projects.

Use Cases

  • Scenario 1: Architecting type-safe builders for complex data structures or UI layouts (like HTML or Compose) to replace verbose and error-prone boilerplate code.
  • Scenario 2: Developing custom Gradle convention plugins and DSLs to streamline build logic across multi-module Android or Backend projects.
  • Scenario 3: Implementing hierarchical configuration systems with strict scope control using @DslMarker to prevent invalid property access in nested DSL blocks.
  • Scenario 4: Migrating legacy Groovy-based Gradle scripts to Kotlin DSL for better IDE support, auto-completion, and refactoring capabilities.
namekotlin-dsl
descriptionKotlin DSL - type-safe builders, Gradle DSL, @DslMarker
version"1.0.0"
sasmp_version"1.3.0"
bonded_agent07-kotlin-dsl
bond_typePRIMARY_BOND
timeout_ms30000
max_attempts3
backoffexponential
initial_delay_ms1000
- nameimmutable
typeboolean
validation"^(gradle|custom|html|config)$"
defaulttrue
levelinfo
events[skill_invoked, type_loaded, error_occurred]

Kotlin DSL Skill

Build type-safe DSLs with Kotlin's language features.

Topics Covered

@DslMarker for Scope Control

@DslMarker
annotation class HtmlDsl

@HtmlDsl
class HTML { fun body(block: Body.() -> Unit) { } }

@HtmlDsl
class Body { fun p(text: String) { } }

// Usage - scoped correctly
html { body { p("Text") } }

Gradle Convention Plugin

// kotlin-library.gradle.kts
plugins { kotlin("jvm"); `java-library` }

kotlin { jvmToolchain(17) }

dependencies {
    testImplementation(kotlin("test"))
    testImplementation("io.mockk:mockk:1.13.9")
}

tasks.test { useJUnitPlatform() }

Type-Safe Config Builder

@ConfigDsl
class ServerConfig private constructor(val host: String, val port: Int) {
    class Builder {
        var host = "localhost"
        var port = 8080
        fun build() = ServerConfig(host, port)
    }
}

fun serverConfig(block: ServerConfig.Builder.() -> Unit) =
    ServerConfig.Builder().apply(block).build()

// Usage
val config = serverConfig { host = "api.example.com"; port = 443 }

Troubleshooting

Issue Resolution
Scope pollution Add @DslMarker annotation
Gradle cache stale Run ./gradlew --stop

Usage

Skill("kotlin-dsl")