Environment variables are an essential part of any application configuration. They allow you to manage application settings outside the code, making it more versatile and secure. In this article, we'll go through how you can set and access environment variables in Kotlin programs.
Accessing Environment Variables
Kotlin, running on the JVM, provides you with the ability to interact with environment variables that are available to your program. You can access them using the System.getenv() method. Here’s an example:
fun main() {
val path = System.getenv("PATH")
println("The system PATH is: $path")
}
In this code snippet, we access the PATH environment variable and print it.
Setting Environment Variables for Local Development
While developing, you may need to set environment variables. There are several ways to do this, depending on your operating system and development environment.
On Windows
To set an environment variable on Windows:
- Go to the Start menu and search for 'Environment Variables'.
- Click on 'Edit the system environment variables'.
- In the System Properties window, click the 'Environment Variables...' button.
- Press 'New...' to create a new environment variable.
- Enter the name and value for your variable and press OK.
On macOS/Linux
You can set environment variables temporarily in your shell with the export command:
export MY_VAR="some_value"
To make them permanent, add the export statement to your shell configuration file, such as ~/.bashrc or ~/.zshrc:
echo 'export MY_VAR="some_value"' >> ~/.bashrc
After doing this, reload the file with:
source ~/.bashrc
Setting Environment Variables in Code (Not Recommended)
While it's generally not recommended due to security reasons, you can set environment variables directly in your Kotlin program if you want to. This can be achieved with Java interoperation, using System.setProperty() instead:
fun main() {
System.setProperty("MY_APP_SETTING", "value")
val mySetting = System.getProperty("MY_APP_SETTING")
println("My App Setting: $mySetting")
}
This approach is more suited for application initialization rather than ongoing configuration since the properties set this way will only exist during the runtime of the application and will not modify system-wide environment variables.
Using .env Files (Recommendation)
.env files are a common way to manage environment variables in development. They take advantage of build tools or libraries to load the variables automatically. In a Kotlin project, you can use a library like java-dotenv:
dependencies {
implementation("io.github.cdimascio:java-dotenv:5.2.2")
}
Create an .env file in your project directory:
MY_SECRET=supersecretvalue
Then, load and access it in your Kotlin program:
import io.github.cdimascio.dotenv.dotenv
fun main() {
val dotenv = dotenv()
val secret = dotenv["MY_SECRET"]
println("The secret is: $secret")
}
Using .env files makes it easy to switch between different configurations for different environments (development, testing, production, etc.) and ensures that sensitive data isn’t hardcoded into the source files.