Environment Setup
Overview
This chapter will guide you through setting up the Kotlin development environment, including JDK installation, IDE configuration, and project creation. We'll cover multiple development environment options, from simple online editors to complete IDE configurations.
System Requirements
Minimum Requirements
- Operating System: Windows 7+, macOS 10.9+, Linux (any modern distribution)
- Memory: 4GB RAM (8GB+ recommended)
- Storage: 2GB available space
- Java: JDK 8 or higher
Recommended Configuration
- Memory: 8GB+ RAM
- Storage: SSD hard drive
- Java: JDK 11 or 17 (LTS versions)
Installing Java Development Kit (JDK)
1. Check Existing Java Version
# Check Java version
java -version
javac -version2. Install JDK
Windows
- Visit Oracle JDK or OpenJDK
- Download the JDK installer for your system
- Run the installer and follow the prompts
- Configure environment variables:
- Add
JAVA_HOMEpointing to JDK installation directory - Add
%JAVA_HOME%\bintoPATH
- Add
macOS
# Install using Homebrew
brew install openjdk@17
# Or download and install manuallyLinux (Ubuntu/Debian)
# Install OpenJDK
sudo apt update
sudo apt install openjdk-17-jdk
# Verify installation
java -versionDevelopment Environment Options
1. IntelliJ IDEA (Recommended)
IntelliJ IDEA is the official IDE for Kotlin, providing the best Kotlin development experience.
Installation Steps
- Visit JetBrains website
- Download Community Edition (free) or Ultimate Edition
- Install and launch IDEA
- Kotlin plugin is pre-installed, no additional configuration needed
Create Kotlin Project
// 1. File -> New -> Project
// 2. Select "Kotlin" -> "Kotlin/JVM"
// 3. Configure project name and location
// 4. Select project SDK (JDK)
// 5. Click "Finish"2. Android Studio
Optimized for Android development, with built-in Kotlin support.
Installation Steps
- Visit Android Developers website
- Download and install Android Studio
- Android SDK will be downloaded automatically on first launch
Create Kotlin Android Project
// 1. Start a new Android Studio project
// 2. Select project template
// 3. Select "Kotlin" in language options
// 4. Configure project details
// 5. Click "Finish"3. Visual Studio Code
Lightweight editor suitable for simple Kotlin development.
Installation Steps
- Install Visual Studio Code
- Install Kotlin extension:
- Open Extensions panel (Ctrl+Shift+X)
- Search "Kotlin Language"
- Install official Kotlin extension
4. Online Editors
Suitable for quick testing and learning.
Kotlin Playground
- Visit: https://play.kotlinlang.org/
- No installation needed, write and run Kotlin code directly in browser
- Supports multiple target platforms (JVM, JS, Native)
Creating Your First Kotlin Project
Using IntelliJ IDEA
- Create New Project
File -> New -> Project -> Kotlin -> Kotlin/JVM- Project Structure
MyKotlinProject/
├── src/
│ └── main/
│ └── kotlin/
│ └── Main.kt
├── .idea/
├── out/
└── MyKotlinProject.iml- Write Hello World Program
// Main.kt
fun main() {
println("Hello, Kotlin!")
}- Run Program
- Click the green triangle on the left of the code
- Or use shortcut Ctrl+Shift+F10 (Windows/Linux) or Cmd+Shift+R (macOS)
Using Command Line
- Create Project Directory
mkdir my-kotlin-project
cd my-kotlin-project- Create Source File
// hello.kt
fun main() {
println("Hello, Kotlin!")
}- Compile and Run
# Compile
kotlinc hello.kt -include-runtime -d hello.jar
# Run
java -jar hello.jarBuild Tool Configuration
Gradle (Recommended)
build.gradle.kts (Kotlin DSL)
plugins {
kotlin("jvm") version "1.9.10"
application
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
}
application {
mainClass.set("MainKt")
}Running Gradle Project
# Build
./gradlew build
# Run
./gradlew run
# Test
./gradlew testMaven
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<kotlin.version>1.9.10</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>Android Development Environment Setup
1. Install Android Studio
Follow the steps mentioned earlier to install Android Studio.
2. Configure Android SDK
// In Android Studio:
// Tools -> SDK Manager
// Install latest Android SDK and build tools3. Create Android Project
// Select "Empty Activity"
// Language select "Kotlin"
// API Level select 21+ (recommended)4. Basic Android Kotlin Code
// MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Kotlin Android extensions
val textView = findViewById<TextView>(R.id.textView)
textView.text = "Hello, Kotlin Android!"
}
}Common Issues and Solutions
1. JDK Version Issues
# Error: Kotlin requires JDK 8+
# Solution: Upgrade JDK version
java -version # Check current version2. Environment Variable Configuration
# Windows
set JAVA_HOME=C:\Program Files\Java\jdk-17
set PATH=%JAVA_HOME%\bin;%PATH%
# macOS/Linux
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH3. IDE Plugin Issues
- Ensure Kotlin plugin is enabled
- Update to latest version
- Restart IDE
4. Compilation Errors
// Common error: kotlinc command not found
// Solution: Install Kotlin compiler
# Using SDKMAN
sdk install kotlin
# Using Homebrew (macOS)
brew install kotlinVerify Installation
Create and run the following test program to verify environment setup:
// test.kt
fun main() {
println("Kotlin version: ${KotlinVersion.CURRENT}")
println("Java version: ${System.getProperty("java.version")}")
println("Environment setup successful!")
// Test basic features
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println("Doubled numbers: $doubled")
}Next Steps
After environment setup is complete, let's start writing our first Kotlin program and learn basic syntax.
Next Chapter: Quick Start
Exercises
- Install JDK and IntelliJ IDEA on your system
- Create a new Kotlin project
- Write and run a "Hello World" program
- Try using the Kotlin Playground online editor
- Configure a Gradle build file and run the project