Kotlin Coroutines: Let it async in

Learn how to create and implement Kotlin Coroutines, how they work, and how to use them in advanced cases.

Bapusaheb Patil
3 min readApr 25, 2019
Image Credits: Codementor

This post was originally published on Codementor.

What are Coroutines?

Think of Coroutines as lightweight threads that are used to perform tasks asynchronously, a.k.a. non-blocking programming.

They are natively supported by many programming languages, such as Go, Python, Perl, Ruby, Kotlin, etc.

A Kotlin Coroutine is a feature in Kotlin that lets you write non-blocking, asynchronous code that doesn’t require context-switching.

Who this tutorial is for?

Although Coroutines are used in general-purpose programming quite often, this article will primarily focus on Coroutines in an Android context.

This article will be a guide on how to implement Coroutines and the know-hows on integrating them into your existing Android app. We’ll be taking a look at how to create Coroutines, how they work, and advanced usages to fine-tune it in an Android app.

The prerequisites are as followed:

  • Knowledge of basic Kotlin syntax;
  • Basic knowledge of RxJava2 (Recommended, not mandatory);
  • Basic asynchronous programming experience in Android development;
  • Android Studio 3.2 or above; and,
  • Kotlin 1.3.0 or above.

Your app on the main thread

If you recently got a new phone, chances are it has a refresh rate of at least 60Hz. This means that your app has 16ms to perform tasks on the Android main thread. Keep in mind that there are phones with higher refresh rates, and this time period will vary.

These tasks are usually performed on the main thread of an Android app:

  • XML parsing
  • View inflation
  • View measurement
  • View positioning

So, as you can see, your app does quite a lot of processing on the main thread, and that’s where the need to perform your tasks on an asynchronous thread arises.

So why Kotlin Coroutines?

Kotlin Coroutines enhance asynchronous programming by being lightweight and essentially faster than a thread as they are stackless. What this means from a multiprocessing perspective, is that Kotlin Coroutines don’t map on the native CPU thread, hence there’s no context-switching on the processor.

Additionally as most phones have at least 4 cores these days, it might be a good idea to put all 4 cores to work!

Adding Kotlin Coroutines to your project

What’s noteworthy when it comes to adding Kotlin Coroutines to your project is that they have been stable since the release of Kotlin 1.3.0. So if you’ve been using any version of Kotlin that’s below 1.3.0, it’s recommended that you upgrade the version in Android Studio IDE. Note: At the time of writing this article, the latest version of Kotlin was 1.3.21.

Here’s how you’d include the latest version of Kotlin in your project-level build.gradle file:

buildscript {
ext.kotlin_version = '1.3.21'
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // Kotlin Gradle Plugin
}
}

To read the rest of the article, click on the following link:

--

--

Bapusaheb Patil
Bapusaheb Patil

Written by Bapusaheb Patil

Award-Winning Designer • Design Book Author • Video Course Creator

No responses yet