IntelliJ IDEA is for Kotlin created by JetBrains (which also created the Kotlin language). To get started, we could use the JetBrains Toolbox App that it’s an easy way to manage JetBrains IDEs.
Setting up a project
The steps for creating a project are very simple, we just need to create a new Project with the following information:
- Name
- Location
- Build System
- Gradle Kotlin is preferred as the build system as this flavor is now the default for new builds .
- Project SDK
- This depends on the project you are going to work on. IntelliJ can download and manage SDKs. Although this is an easy way, managing it by yourself is preferred. There are different tools to do this but one I like is asdf .
- There are different distributions of Java SDK, to learn more about that, this website is an interesting read. TLDR; choose Adoptium Eclipse Temurin OpenJDK builds.
Project settings can be accessed from File>Project Settings in the menu bar. In this section, we can configure the SDK among other settings.
Folder/File Structure
├── .gradle
├── .idea
├── gradle
├── src
│ └── main
│ └── kotlin
│ └── Main.kt
├── build.gradle.kts
├── gradle.properties
├── settings.gradle.kts
└── External Libraries- .gradle
- Contains caches used by the build
- .idea
- Contains settings files for the project and the IDE
- gradle
- It uses the
gradlewandgradlew.batfor Gradle Wrapper, a self-contained copy of the Gradle build system that it can be used without having to install Gradle on the computer.
- It uses the
- build.gradle.kts | gradle.properties | settings.gradle
- These files are configuration files for the Gradle build system. They define various pieces of information such as the project name, language level, project modules, and dependencies in the project.
- External Libraries
- Contains information about libraries the project depends on. IntelliJ automatically adds Java and several Kotlin standard library packages as dependencies for a new project.
Project Structure
Running a Kotlin file
//Main.kt
fun main(args: Array<String>) {
println("Hello World!");
}IntelliJ will display a green ▶️ icon, known as the “run button” to the left of the main function. The first option should be Run ‘MainKt’. After clicking this one, we should see a new console window at the bottom that should display a classic “Hello World!” message.
Internal steps on running the code
- IntelliJ compiles the Kotlin code using the Kotlin-jvm compiler into bytecode.
- IntelliJ executes the generated bytecode.
- JVM is terminated and IntelliJ prints the termination status in the console.
Kotlin REPL
IntelliJ provides a tool for testing code without having to create a file. It can be opened by selecting Tools > Kotlin > Kotlin REPL in the menu bar.
Inspecting Kotlin Bytecode
If curious about how Kotlin decisions from syntax sugar affect the resulting bytecode generated to run on the JVM, you can use the Kotlin bytecode tool window by selecting Tools > Kotlin > Show Kotlin Bytecode.
Since Bytecode can be hard to read/understand, there is a Decompile button that translates the bytecode into Java.
Why Use IntelliJ?
Well, that’s a good question. Although Kotlin can be installed without the need for IntelliJ and “there is support” for other IDEs; the experience is not the same. It makes sense as JetBrains, the company behind Kotlin, is a company that sells IDEs professionally ¯\_(ツ)_/¯
