Kotlin Coroutine Mechanisms: Beginner-Friendly Workshop Setup
IntelliJ, Kotlin Koans lab setup, and Coroutine Debugger setup
Hello! Welcome to the Coding Kinetics workshop “Kotlin Coroutine Mechanisms”. In this blurb, you’ll learn how to set up your computer to follow along for the Chicago public community workshop in January 2025. This workshop is inspired by the recent Droidcon NYC 2024 talk, “Kotlin Coroutine Mechanisms”. Watch the full talk here for a sneak preview of the seminar.
Experienced programmers can scroll down to the “Setting up the Coroutine Debugger” section to set up the Coroutine Debugger on their IDE.
In this blurb, you will:
- Install your IntelliJ IDE setup
- Create a Kotlin Project
- Access the Kotlin Koans tutorial
- Configure your Coroutine Debugger
Totally new to programming or Kotlin?
No problem. Get started learning and working with Kotlin for free. If you get stuck and want to see other solutions or examples, get to googling to see how others have accomplished the same thing. Google is your friend.
Make sure to learn the absolute basics of Kotlin syntax before attending the code lab. I will try to make it as beginner-friendly as possible. But we are introducing the idea of concurrency in the lab, and we only have an hour to run the lab. Be sure to follow the instructions below, and especially run through the Learning Kotlin right in the IDE section for a free Kotlin tutorial right in your IDE.
Download & Set Up IntelliJ IDEA Community Edition
An IDE stands for “Interactive Developer Environment”. IDEs are great because you can write and run code with little fuss/setup.
Head over to the JetBrains link https://www.jetbrains.com/idea/download/. Ensure that the website detects your proper operating system. In my case, I’m using a MacOS, so the website has directed me to download the appropriate format.
Scroll down to the IntelliJ IDEA Community Edition (CE). That is the IDE that is free for everyone!
Once you’ve downloaded IntelliJ IDEA, open it up to get to the first screen:
Select “New Project” and name your project “KotlinCoroutinesMechanisms”.
Give your project a few minutes to get set up. It takes up a lot of processing power on your computer! You can view the bottom left blue bar to see what the IDE is trying to do, but it should be able to configure itself without you have to do anything.
Once loaded, you should see sample code in the main panel to the right, and a project directory to the left. If you select the target icon at the top of the Project directory panel, you can quickly navigate to the current file you’re looking at.
You can press play to run the code. You should see a new panel pop up at the bottom. In my case, I ended up with the following message:
Execution failed for task ':compileKotlin'.
> Inconsistent JVM-target compatibility detected for tasks 'compileJava' (23) and 'compileKotlin' (22).
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 6s
1 actionable task: 1 executed
Ope. Incompatibility detected for compileJava
(23) and compileKotlin
(22). I googled the error message itself exactly as-is and found the following the following suggestion, which fixed my issue.
Navigate to build.gradle.kts
and add the following code above the dependencies section:
kotlin {
jvmToolchain(21)
}
dependencies {
...
}
A small elephant popup should show. Click that to sync Gradle.
Once it builds successfully, navigate back to the original file and press play again. Alright! I now get:
/Users/amandahinchman-dominguez/Library/Java/JavaVirtualMachines/openjdk-23.0.1/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=50284:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /Users/amandahinchman-dominguez/IdeaProjects/KotlinCoroutinesMechanisms/build/classes/kotlin/main:/Users/amandahinchman-dominguez/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.0.21/618b539767b4899b4660a83006e052b63f1db551/kotlin-stdlib-2.0.21.jar:/Users/amandahinchman-dominguez/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar org.example.MainKt
Hello, Kotlin!
i = 1
i = 2
i = 3
i = 4
i = 5
Process finished with exit code 0
We’re ready to get cooking! Feel free to play with the code in the IDE to make different outputs and experiment as you please.
Learning Kotlin via Kotlin Koans
There are two options for learning Kotlin via Kotlin Koans — online, or in the IDE via plugins. Pick your preferred path! It’s worth noting I had a few IDE issues trying to import the plugin right in my IntelliJ, but give it a try and see if it works for you.
Setting up the Coroutine Debugger
Check for Dispatchers.Main
documentation for your respective platform, but this lab makes use of IntelliJ Studio. If desired, download this GitHub starter project to follow along.
In the world of coroutines, logging is your friend — but we’re also trying out the functionality offered by IntelliJ/AS IDEs for coroutine debugging.
The following function is added to your project for convenience:
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
You print statements with log($input)
. Then, edit your run configurations by adding -Dkotlinx.coroutines.debug
to your VM options:
Classes can be run in debug mode, and a Coroutines tab can be opened up in the debug console to observe the state of the coroutine:
That should be it! If you’d like to start playing around with coroutines, feel free to access previous blurbs on Kotlin Coroutine Mechanisms:
- Kotlin Coroutine Mechanisms part 1: runBlocking v. launch
- Kotlin Coroutine Mechanisms part 2: launch v. async
- Kotlin Coroutine Mechanisms part 3: swapping CoroutineContext
See you at the next community workshop!