Using kotlinx.coroutines in IntelliJ IDEA project
Asked Answered
V

3

44

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?

Venturous answered 26/9, 2018 at 16:24 Comment(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.Michelson
More precisely, the core Kotlin does contain coroutines code, but only the fundamental low-level abstractions. You must opt into the full coroutine API.Biased
A
73

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.

Annice answered 28/9, 2018 at 1:15 Comment(9)
@IgorGanapolsky in Android Studio you should add kotlinx.coroutines as a Gradle dependency.Annice
Nope that doesn't cut itPillage
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 hereWhiffen
@PanosGr, thanks, I've added an explanation of how to find the latest version.Annice
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 #68150501Baikal
@Baikal 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.Bandbox
Nope. Doesn't work. i'm spending hours on it already...Yanina
I am able to resolve the issue by adding the dependency as per this link github.com/Kotlin/kotlinx.coroutines/blob/master/…Particle
@Ilya, what is the reson to check the boxes "Transitive dependencies" and "Sources"??Plotinus
F
43

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

Famish answered 26/9, 2019 at 18:49 Comment(4)
Very weird, not sure why by just adding a gradle dependency it is being added (Just like their docs mention it)Immaculate
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.Enriquetaenriquez
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".Bandbox
please refer to this link for the steps to add the kotlinx.coroutines-core dependency github.com/Kotlin/kotlinx.coroutines/blob/master/…Particle
M
2

If you are using gradle build system, just add implementation to build.gradle(.kts)

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
    // ......
}
Medicaid answered 24/9, 2023 at 15:40 Comment(1)
Note, with this dependency there was no documentation being shown for anything from the coroutines library, so I had to change it to "org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.7.3". Now I can see the documentation again. It may have been a bug though, or the library without the "jvm" contained compiled code only without documentation.In

© 2022 - 2024 — McMap. All rights reserved.