Kotlin Application Configuration: Libraries and Approaches

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Kotlin Application Configuration: Modern Approaches and Libraries



Proper configuration is the key to flexibility and security of any application. In the Kotlin ecosystem, developers have access to a wide range of tools: from classic HOCON files to powerful frameworks with DI support. In this article, we will cover key libraries, patterns, and best practices for managing configuration in Kotlin applications.



1. Typesafe Config (HOCON) — Industry Standard



Typesafe Config is the most popular library for configuring JVM applications. It uses the HOCON (Human-Optimized Config Object Notation) format, which supports inheritance, file inclusion, and environment variables.



Adding Dependency (Gradle)

dependencies {    implementation("com.typesafe:config:1.4.3")}


Example Configuration File (application.conf)

app {  name = "MyKotlinApp"  version = "1.0.0"  database {    url = "jdbc:postgresql://localhost:5432/mydb"    user = ${?DB_USER}   # overridden from env    password = ${?DB_PASSWORD}  }  features {    enableLogging = true    maxRetries = 3  }}


Loading in Kotlin

import com.typesafe.config.ConfigFactory

fun main() { val config = ConfigFactory.load() val appName = config.getString("app.name") val dbUrl = config.getString("app.database.url") val retries = config.getInt("app.features.maxRetries") println("App: $appName, DB: $dbUrl, Retries: $retries")}


Advantages: lightweight, no DI required, supports ${?ENV_VAR} for environment variable substitution. Great for microservices and CLI utilities.



2. Spring Boot + @ConfigurationProperties



If you are using Spring Boot, the built-in configuration support via application.yml or application.properties is an ideal choice. The library automatically maps properties to Kotlin data classes.



Example application.yml

app:  name: "SpringKotlinApp"  database:    url: "jdbc:mysql://localhost:3306/db"    pool-size: 10  retry:    enabled: true    max-attempts: 3


Kotlin Data Class for Configuration

import org.springframework.boot.context.properties.ConfigurationPropertiesimport org.springframework.stereotype.Component

@Component@ConfigurationProperties(prefix = "app")data class AppConfig( val name: String = "", val database: DatabaseConfig = DatabaseConfig(), val retry: RetryConfig = RetryConfig())

data class DatabaseConfig( val url: String = "", val poolSize: Int = 5)

data class RetryConfig( val enabled: Boolean = false, val maxAttempts: Int = 1)


Usage in a Service

@Serviceclass UserService(private val config: AppConfig) {    fun printConfig() {        println("DB URL: ${config.database.url}")        println("Retry enabled: ${config.retry.enabled}")    }}


Pros: integration with DI, validation via @Validated, profile support (dev/prod). Cons: tied to Spring.



3. Ktor + HOCON (for Ktor Applications)



For server applications on Ktor, configuration is often built using the built-in HOCONApplicationConfig module. This is a lightweight alternative to Spring.



Setup

dependencies {    implementation("io.ktor:ktor-server-core:2.3.0")    implementation("io.ktor:ktor-server-config-hocon:2.3.0")}


File application.conf (Ktor)

ktor {  deployment {    port = 8080    host = "0.0.0.0"  }  application {    modules = [ "com.example.ApplicationKt.module" ]  }  custom {    apiKey = ${?API_KEY}    timeoutMs = 5000  }}


Loading Configuration in Code

fun App                    

Recommendations