Loads .env files into runtime via DSL or fluent builder, honoring system env precedence; supports quoted/multiline values, comments, escapes, duplicates, and ignore options.
A Kotlin Multiplatform library for loading environment variables from .env files.
Inspired also from dotenv-kotlin
and dotenv-java, but with multi-platform support.
implementation("io.paoloconte:kotenv:1.0.2")val env = kotenv {
directory = Path("path", "to", "directory")
filename = "prod.env"
ignoreIfMissing = true
ignoreIfMalformed = true
ignoreIfEmpty = true
}
val value = env["MY_VARIABLE"] ?: "default"val env = Kotenv.builder()
.directory(Path("path", "to", "directory"))
.filename("dev.env")
.ignoreIfMissing()
.ignoreIfMalformed()
.ignoreIfEmpty()
.build()
val value = env["MY_VARIABLE"] ?: "default"The directory parameter uses kotlinx.io.files.Path. Use the vararg constructor to build platform-independent paths:
import kotlinx.io.files.Path| Option | Default | Description |
|---|---|---|
filename |
.env |
The name of the environment file to load |
directory |
current | The directory where the environment file is located |
ignoreIfMissing |
false |
If true, no exception is thrown when the file is not found |
ignoreIfMalformed |
false |
If true, malformed lines are silently ignored instead of throwing an exception |
ignoreIfEmpty |
false |
If true, variables with empty values (e.g., VAR=) are excluded and return null; Also applies to malformed |
.env file values#) and trailing comments\n, \t, \r, \", \\)# Comment
SIMPLE_VAR=value
QUOTED_VAR="value with spaces"
MULTI_LINE="line1
line2"
EMPTY_VAR=
TRAILING_COMMENT=value # this is ignored
A Kotlin Multiplatform library for loading environment variables from .env files.
Inspired also from dotenv-kotlin
and dotenv-java, but with multi-platform support.
implementation("io.paoloconte:kotenv:1.0.2")val env = kotenv {
directory = Path("path", "to", "directory")
filename = "prod.env"
ignoreIfMissing = true
ignoreIfMalformed = true
ignoreIfEmpty = true
}
val value = env["MY_VARIABLE"] ?: "default"val env = Kotenv.builder()
.directory(Path("path", "to", "directory"))
.filename("dev.env")
.ignoreIfMissing()
.ignoreIfMalformed()
.ignoreIfEmpty()
.build()
val value = env["MY_VARIABLE"] ?: "default"The directory parameter uses kotlinx.io.files.Path. Use the vararg constructor to build platform-independent paths:
import kotlinx.io.files.Path| Option | Default | Description |
|---|---|---|
filename |
.env |
The name of the environment file to load |
directory |
current | The directory where the environment file is located |
ignoreIfMissing |
false |
If true, no exception is thrown when the file is not found |
ignoreIfMalformed |
false |
If true, malformed lines are silently ignored instead of throwing an exception |
ignoreIfEmpty |
false |
If true, variables with empty values (e.g., VAR=) are excluded and return null; Also applies to malformed |
.env file values#) and trailing comments\n, \t, \r, \", \\)# Comment
SIMPLE_VAR=value
QUOTED_VAR="value with spaces"
MULTI_LINE="line1
line2"
EMPTY_VAR=
TRAILING_COMMENT=value # this is ignored