Kotlin Application Configuration: Hoplite Library

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

What is Hoplite and why is it needed?

Hoplite is a lightweight, type-safe configuration library for Kotlin applications. It solves the problem of manually parsing configuration files (YAML, JSON, TOML, HOCON) and converting them into strictly typed Kotlin objects. Instead of writing dozens of lines of code to read application.conf, you define a data class, and Hoplite deserializes the configuration with on-the-fly validation.

How to install

Add the dependency to build.gradle.kts:

repositories {    mavenCentral()}

dependencies { implementation("com.sksamuel.hoplite:hoplite-core:2.7.5") implementation("com.sksamuel.hoplite:hoplite-yaml:2.7.5") // for YAML}

Main features

  • Type safety: automatic conversion of strings to Int, Boolean, LocalDate, enum, and nested objects.
  • Validation: built-in annotations @Range, @Pattern, @NotEmpty and custom validators.
  • Format support: YAML, JSON, TOML, HOCON, .properties and environment variables.
  • Profiles: switching between dev/prod configurations without code duplication.
  • Secrets: integration with Vault and environment variables for passwords.

Kotlin code example

import com.sksamuel.hoplite.ConfigLoaderimport com.sksamuel.hoplite.yaml.YamlPropertySource

data class DatabaseConfig( val host: String, val port: Int = 5432, val credentials: Credentials)

data class Credentials( val username: String, @com.sksamuel.hoplite.validator.Secret val password: String)

fun main() { val config = ConfigLoader.Builder() .addSource(YamlPropertySource("config.yaml")) .build() .loadConfigOrThrow<DatabaseConfig>()

println("Connecting to ${config.host}:${config.port}") println("User: ${config.credentials.username}")}

When to use

Hoplite is ideal for: microservices on Ktor or Spring Boot, CLI utilities that need strict typing, projects requiring separation into dev/staging/prod profiles, and any Kotlin applications where you want to avoid ConfigFactory.parseResources() and manual mapping. It is not recommended for simple scripts with one or two parameters — System.getenv() is sufficient for those.

Conclusion

Hoplite makes configuration in Kotlin as safe and elegant as the language itself. If you are tired of ClassCastException when reading configs — give this library a try.

Recommendations