34

I am trying to learn coroutines and so I fire up IntelliJ and create a scratch file. But when I type in my coroutines I get compiler complaints such as runBlocking is an unresolved reference. So this is not an android project or any such thing. Just a scratch file in a basic Kotlin project.

How do I bring in the coroutine stuff so I stop getting errors?

Ilya
  • 21,871
  • 8
  • 73
  • 92
salyela
  • 1,325
  • 3
  • 14
  • 26
  • 2
    Did you add a dependency in your project to `kotlinx-coroutines-core`? They are bundled separately from Kotlin -- sounds like it will be true in Kotlin 1.3 as well. – Geoffrey Wiseman Sep 26 '18 at 16:55
  • More precisely, the core Kotlin does contain coroutines code, but only the fundamental low-level abstractions. You must opt into the full coroutine API. – Marko Topolnik Sep 27 '18 at 07:42

2 Answers2

62

runBlocking and other high-level coroutine utilities are not in the Kotlin standard library, but instead are a part of the library kotlinx.coroutines.

To use this library in your project you must download its binaries and add a dependency on them to the project. Usually declaring a library dependency is a line or couple of lines in a build file, if you use build systems like Gradle or Maven. However in a plain IntelliJ project it's possible to get that library from Maven Central almost without hassle:

  • Open project structure
  • In the "Modules" page select a module which you use as a context of the scratch file (I suppose there will be just one module).
  • Switch to "Dependencies" tab and hit the plus button.
  • then in a context menu select "Library" -> "From Maven"
  • paste maven coordinates of the kotlinx.coroutines library artifact:

    org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3

    where 1.3.3 is the version of that library. You can find the latest available version here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/README.md

  • be sure to check "Transitive dependencies" and "Sources" boxes.

After hitting OK the library will be downloaded from Maven Central repository with all its dependencies and added to your module. Then it will be possible to use runBlocking in your project or scratch files.

Ilya
  • 21,871
  • 8
  • 73
  • 92
  • @IgorGanapolsky in Android Studio you should add `kotlinx.coroutines` as a Gradle dependency. – Ilya Nov 21 '19 at 17:07
  • Nope that doesn't cut it – IgorGanapolsky Nov 21 '19 at 17:14
  • It worked for me. The only necessary change is to update to the latest version. As of today it is `:1.3.3` but you may find the [latest versions here](https://github.com/Kotlin/kotlinx.coroutines#gradle) – Panos Gr Jan 19 '20 at 12:39
  • @PanosGr, thanks, I've added an explanation of how to find the latest version. – Ilya Jan 20 '20 at 16:19
  • 18
    It didn't work for me. After wasting a lot of time I found the answer in the Kotlin Slack channel. I had to use `org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.0`. For more details check this question https://stackoverflow.com/questions/68150001/unresolved-reference-kotlinx-in-intellijidea-even-after-adding-the-library – nilTheDev Jul 09 '21 at 07:14
  • @nilTheDev Yeah, at least that was so in my case. I was trying to add it to a Kotlin console project, and adding `kotlinx-coroutines-core` did not solve the dependency. Adding `kotlinx-coroutines-core-jvm` did. – Damn Vegetables Jan 01 '22 at 10:09
  • Nope. Doesn't work. i'm spending hours on it already... – Avi Tshuva Apr 04 '22 at 16:42
  • I am able to resolve the issue by adding the dependency as per this link https://github.com/Kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects – Saravanan May 10 '22 at 04:56
  • 1
    @Ilya, what is the reson to check the boxes "Transitive dependencies" and "Sources"?? – Prateek Gupta Aug 13 '22 at 08:04
40

You should add kotlin coroutines library to your project. The simplest way to do it is to get it from Maven repo. At this moment actual version of library is 1.3.2 The address of library in maven repo you could find here

At moment of writing the address of library is

org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2

In plain IDEA IntelliJ project you should make following steps:

1) Go to project structure enter image description here

2) Then go to Modules page and Dependencies Tab enter image description here

3) Press "+" button. Select library from "Maven"

enter image description here

4) In search bar use address org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2 of library in maven repo and add it. enter image description here

5) Press OK then Apply. And recompile your project. That is it. Now you could use coroutines in your project.

enter image description here

Leontsev Anton
  • 727
  • 7
  • 12
  • 1
    Very weird, not sure why by just adding a gradle dependency it is being added (Just like their docs mention it) – Gurupad Mamadapur Dec 18 '19 at 14:25
  • 3
    We can just type org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm in search section of "Library from maven" and intellij will show latest versions to choose from. Remember to search "core-jvm" not just "core" for intellij Idea. – Sharad Oct 15 '21 at 23:11
  • 1
    You need update this answer. I tested it with the latest version of IntelliJ, and the Library menu has no context menu. It just opens a "Choose Libraries" dialogue. There is "New Library" at the bottom of it, and that button has a context menu that has "From Maven". – Damn Vegetables Jan 01 '22 at 09:51
  • please refer to this link for the steps to add the `kotlinx.coroutines-core` dependency https://github.com/Kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects – Saravanan May 10 '22 at 04:55