A Kotlin Multiplatform code generation library for programmatically creating Kotlin source files.
For simple code generation cases, multi-line strings work really well. However, if you want to build more complex code, or have the ability to progressively and conditionally build the code, or make testing the code output easier, then KotlinGen can help. Kotlin Gen provides a type-safe API for generating Kotlin code. Instead of string concatenation or templates, you can build Kotlin declarations using a fluent API that ensures proper formatting, indentation, and syntax.
- Type-safe code generation: Build Kotlin code using classes and builders
- Multiplatform support: Works on JVM, Android, iOS, Linux, Windows, and macOS
- Comprehensive: Supports classes, interfaces, functions, properties, and more
- Automatic formatting: Handles indentation, line breaks, and keyword escaping
- Import management: Automatically organize and sort imports
dependencies {
implementation("dev.ktool:kotlin-gen:1.0.0")
}val file = KotlinFile("com.example") {
+Class("User") {
+Property("id", type = LongType)
+Property("name", type = StringType)
}
}
println(file.render())Output:
package com.example
class User {
val id: Long
val name: String
}val file = KotlinFile("com.example") {
+Function("add", returnType = IntType) {
+Parameter("a", IntType)
+Parameter("b", IntType)
body = ExpressionBody("a + b")
}
}
println(file.render())
// Output: fun add(a: Int, b: Int): Int = a + bval file = KotlinFile("com.example") {
+Function("isEven", returnType = BooleanType) {
receiver = IntType
body = ExpressionBody("this % 2 == 0")
}
}
println(file.render())
// Output: fun Int.isEven(): Boolean = this % 2 == 0val file = KotlinFile("com.example") {
+Interface("Repository") {
+TypeParameter("T")
+Function("save") {
+Parameter("entity", Type("T"))
}
}
}
println(file.render())Output:
interface Repository<T> {
fun save(entity: T)
}val file = KotlinFile("com.example.domain") {
+Import("kotlin.collections.List")
+Import("kotlin.collections.Map")
+Class("Product", modifiers = listOf(Modifer.Data)) {
primaryConstructor = PrimaryConstructor {
+Property("id", type = StringType)
+Property("name", type = StringType)
+Property("price", type = DoubleType)
}
}
}
println(file.render())Output:
package com.example.domain
import kotlin.collections.List
import kotlin.collections.Map
data class Product(val id: String, val name: String, val price: Double)Represents a complete Kotlin source file with package declaration, imports, and top-level declarations.
- Class: Standard and data classes with properties, functions, and init blocks
- Interface: Interfaces with abstract functions and properties
- Object: Singleton objects
- Function: Top-level and member functions with various modifiers
- Property: Top-level and member properties with getters/setters
- TypeAlias: Type aliases
- Type: Represents Kotlin types including generics
- TypeParameter: Generic type parameters with variance
- Parameter: Function parameters with default values
- Modifier: Access modifiers (public, private, etc.) and other modifiers (override, suspend, etc.)
- ExpressionBody: Single-expression function bodies (
= expression) - FunctionBlock: Multi-statement function bodies with braces
- Automatic keyword escaping: Names that conflict with Kotlin keywords are automatically wrapped in backticks
- Import sorting: Imports are automatically sorted alphabetically
- Proper indentation: All nested structures are properly indented
- Init blocks: Support for class initialization blocks
- Primary constructors: Constructor parameters with properties
- JVM
- Android (API 21+)
- iOS (x64, ARM64, Simulator ARM64)
- Linux (x64)
- Windows (mingw x64)
- macOS (x64, ARM64)
Apache License 2.0
Aaron Freeman (aaron@ktool.dev)