Problem getting coroutines working

Very new to Kotlin here, I’m trying to run coroutines in a console application and not having any luck at all. I’m running a fresh install of IntelliJ 2020.2 Community Windows edition, and both it and plugins etc are all up to date.

I’m following the Your first coroutine with Kotlin tutorial, which appears to be out-of-date and written before the Project Wizard started including gradle files written in Kotlin. In any case, my build.gradle.kts now contains these sections:

repositories {
dependencies {

The source code in that article isn’t very good either, as the first sample omits both main and the require import, so I’m using the code from the Coroutine Basics page instead:

import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch { // launch a new coroutine in background and continue
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    println("Hello,") // main thread continues while coroutine is delayed
    Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive

This results in “Unresolved reference” errors for both GlobalScope and delay.

I’ve searched all over the net for solution, to no avail. It seems that coroutines have gone through a lot of changes over the past few years, because there’s tons of info out that there that’s obsolete, out of date and just plain wrong.

I feel like I’m missing something very basic and would greatly appreciate a point in the right direction.

Ok, that’s strange…the solution was to Load Gradle Changes by clicking on the icon that pops up when you edit it (or Control-Shift-O).

Is this normal? I would have thought that any changes would have automatically been picked up, if not at the next build then certainly a rebuild?

1 Like

I’m not 100% sure where in the settings this is but if you look under gradle there should be an option that says something like “auto reload settings”.

I thought this is enabled by default but maybe I’m wrong. The reason you might want to disable this, is that a reaload might take some time (~1 minute or so) for larger more complicated build setups (multiplatform/multiple projectes in 1 gradle build) so changing something in the gradle scripts get’s really tedious if it reloads everything before you are done with your changes.

1 Like

Thanks Wasabi375, looks like the option you’re talking about is “Settings -> Build Tools -> Reload project after changes in the build script”, which in my fresh install was set to “External changes”. Setting it to “Any changes” causes the changes to be applied without the need for a manual re-load.

The default setting is a bit mystifying if you ask me, why would anyone want external changes automatically applied but not those made from within the IDE itself? And if I’ve made changes to the gradle file then I fully expect the first build to take longer so as to accommodate them. At the very least the article in question should have mentioned this, given that the audience it’s targeting (i.e. new users like myself) are going to be unlikely to know any of this.

Anyway, working now, like I said. Thanks again for the help!

1 Like

My guess is that we want the build file to always be reloaded when we switch branches in GIT, which is an “external program”. As IDEA has auto-save the script might be saved in an inconsistent state while we update it, so it might not be the best use of computational time to reload it every time IDEA decides to save, instead the user manually tells when he is done.

Personally I use manual reload because Gradle reload is heavy and when editing a buildscript I don’t want everything to become slow and slugish because a reload (that will fail) is triggered.

Have a nice day!