Skip to content

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
  • Memory: 8GB+ RAM
  • Storage: SSD hard drive
  • Java: JDK 11 or 17 (LTS versions)

Installing Java Development Kit (JDK)

1. Check Existing Java Version

bash
# Check Java version
java -version
javac -version

2. Install JDK

Windows

  1. Visit Oracle JDK or OpenJDK
  2. Download the JDK installer for your system
  3. Run the installer and follow the prompts
  4. Configure environment variables:
    • Add JAVA_HOME pointing to JDK installation directory
    • Add %JAVA_HOME%\bin to PATH

macOS

bash
# Install using Homebrew
brew install openjdk@17

# Or download and install manually

Linux (Ubuntu/Debian)

bash
# Install OpenJDK
sudo apt update
sudo apt install openjdk-17-jdk

# Verify installation
java -version

Development Environment Options

IntelliJ IDEA is the official IDE for Kotlin, providing the best Kotlin development experience.

Installation Steps

  1. Visit JetBrains website
  2. Download Community Edition (free) or Ultimate Edition
  3. Install and launch IDEA
  4. Kotlin plugin is pre-installed, no additional configuration needed

Create Kotlin Project

kotlin
// 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

  1. Visit Android Developers website
  2. Download and install Android Studio
  3. Android SDK will be downloaded automatically on first launch

Create Kotlin Android Project

kotlin
// 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

  1. Install Visual Studio Code
  2. 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

  1. Create New Project
File -> New -> Project -> Kotlin -> Kotlin/JVM
  1. Project Structure
MyKotlinProject/
├── src/
│   └── main/
│       └── kotlin/
│           └── Main.kt
├── .idea/
├── out/
└── MyKotlinProject.iml
  1. Write Hello World Program
kotlin
// Main.kt
fun main() {
    println("Hello, Kotlin!")
}
  1. 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

  1. Create Project Directory
bash
mkdir my-kotlin-project
cd my-kotlin-project
  1. Create Source File
kotlin
// hello.kt
fun main() {
    println("Hello, Kotlin!")
}
  1. Compile and Run
bash
# Compile
kotlinc hello.kt -include-runtime -d hello.jar

# Run
java -jar hello.jar

Build Tool Configuration

build.gradle.kts (Kotlin DSL)

kotlin
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

bash
# Build
./gradlew build

# Run
./gradlew run

# Test
./gradlew test

Maven

pom.xml

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

kotlin
// In Android Studio:
// Tools -> SDK Manager
// Install latest Android SDK and build tools

3. Create Android Project

kotlin
// Select "Empty Activity"
// Language select "Kotlin"
// API Level select 21+ (recommended)

4. Basic Android Kotlin Code

kotlin
// 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

bash
# Error: Kotlin requires JDK 8+
# Solution: Upgrade JDK version
java -version  # Check current version

2. Environment Variable Configuration

bash
# 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:$PATH

3. IDE Plugin Issues

  • Ensure Kotlin plugin is enabled
  • Update to latest version
  • Restart IDE

4. Compilation Errors

kotlin
// Common error: kotlinc command not found
// Solution: Install Kotlin compiler
# Using SDKMAN
sdk install kotlin

# Using Homebrew (macOS)
brew install kotlin

Verify Installation

Create and run the following test program to verify environment setup:

kotlin
// 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

  1. Install JDK and IntelliJ IDEA on your system
  2. Create a new Kotlin project
  3. Write and run a "Hello World" program
  4. Try using the Kotlin Playground online editor
  5. Configure a Gradle build file and run the project

Content is for learning and research only.