Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using kotlinx.coroutines in IntelliJ IDEA project

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?

like image 215
salyela Avatar asked Sep 26 '18 16:09

salyela


People also ask

How do I add a project to Kotlinx?

In the Project Structure dialog, go to Project Settings | Libraries, press "+" button and select "From Maven..." in the popup. In the dialog that appears, enter the name and version of the library you need, which you can find out in the documentation. (For the current version of the core library of kotlinx.

How do I add coroutines to Kotlin project?

Go to Tools → Kotlin → Configure Kotlin Plugin Updates, select “Stable” in the Update channel drop-down list, and then click Check for updates. We are adding coroutines-core along with coroutines-android. Now, sync your project with gradle files and you are ready use the latest Coroutines. Thanks for reading!


2 Answers

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.

like image 173
Ilya Avatar answered Sep 19 '22 03:09

Ilya


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

like image 28
Leontsev Anton Avatar answered Sep 21 '22 03:09

Leontsev Anton