If you landed here I’m guessing you had a bit of an issue adding the kotlinx.coroutines package to you project. Or possibly are looking to add it. I’m not sure if I was doing something incorrectly or just don’t know enough about how Intellij works yet, but I ran into more difficulties than I expected. Below is the list of steps I took to be able to utilize coroutine classes like ‘launch’ in my project. Hopefully, it helps or at the very least gives you something to go on if you are having issues as well.
Adding the library to the project
In the project view right click (assuming on a Windows machine) on the project and in the popup menu select ‘Open Module Settings’
The first thing that is needed is to import the library. In the popup window select ‘Libraries’ from the Project Settings section. Then select the ‘+’ sign and select ‘From Maven’ in the drop down.
Then in the Maven Repository Search Bar type kotlinx-coroutines-core and click the magnifying glass to search for the dependencies. After that you should see the case below or something similar. There where 190 different versions found in my case. Then select the one that best suites your needs and hit OK. If you are curious to learn more. The GitHub page for the project explains the difference between the different version – Kotlinx-Coroutines.
After that you should now see the library added to your Libraries section.
Adding the module to your projects main module
Next go to the Modules section in the Project Structure window and select the project that the module should be added to. In my case it is the main module under my app.
In the far right pane select the ‘+’ sign again but this time we are importing the library.
Finally select the coroutines library from the popup window.
After that you should now see the library added to your main module.
That should be enough to start referencing the project in your code files. However, if you are using Gradle you might run into a strange issue where you can reference the classes in the library but when you go to build the app you get ‘Unknown reference kotlinx’. This is because we never told Gradle about the dependency so we need to add it to the build.gradle.kts file if you are using the Kotlin version of Gradle. If you are using the Groovy version of Gradle the file should be called the same ‘build.gradle’ but with a different extension
Configuring the build.gradle file
First lets add the missing dependency. In project explorer window select build.gradle.kts
Now you need to know what the full name of the module is called. To find that you can open up the Project Settings window again by right clicking on the project in project explorer. After that select ‘Open Module Settings’ click on libraries and click on the coroutines.core library that was added.
The full name of the module is listed in the right window pane.
Add that name as an implementation dependency under the dependencies section in the build.gradle file.
After that project should build. Cheers!