Impressions of Google I/O 2019: Kotlin Coroutines
By: Lauren Yew
Tags: Android App Development, AndroidDev, Android, Kotlin Coroutines, Jetpack

Google I/O 2019 announced that Google is officially giving first class support to Kotlin and Kotlin Coroutines! This means that the new Android libraries will be written for Kotlin first (in fact, Jetpack Compose — used for Declarative UI Patterns — will be Kotlin only), and that the Jetpack libraries also include Kotlin Coroutines support. Google developers highly recommend using Kotlin Coroutines for doing asynchronous work and when integrating Jetpack.

What are Kotlin Coroutines?

Kotlin Coroutines is a stable JetBrains library for coroutines that makes it easy to do asynchronous programming and threading. Coroutines use suspend functions to encapsulate some amount of work that can be run on an asynchronous pool of threads. The Kotlin Coroutines library handles the async threading work for you, so you can do your async calls without callbacks, extra subscribers, or observers to get the results back.

Kotlin Coroutines in Jetpack

Jetpack has several libraries you can use today that include coroutine support.

WorkManager supports coroutines in its stable release. You can use WorkManager with coroutines to handle long background jobs, such as syncing local data with the web-server and handling cancellations.

Room 2.1 also includes coroutine support. Simply:

“update your DAO (query) methods to use suspension functions..(and) the (Room) generated code will make sure that your data gets inserted but also that this happens off of the UI thread” https://medium.com/androiddevelopers/room-coroutines-422b786dc4c5

Not only does Room handle doing transactions on a background thread for you, but it handles cancellations for you as well.

Worried about doing parallel database calls and hitting concurrency problems? According to the Jetpack devs at Google I/O, Room will handle database concurrency for you as well.

You can use LiveData with Kotlin Coroutines to create “lifecycle aware coroutine scopes”. For more on coroutine scope check out the Kotlin Coroutines Github page. Basically, using ViewModels, you can associate your coroutine scope to the view model lifecycle, and when you no longer need your ViewModel, all the associated coroutines running with that ViewModel can be automatically cancelled, so their encapsulated by that ViewModel’s lifecycle. You can also use LifecycleScope to associate your coroutine set to any view lifecycle.

LiveData also has another added benefit of being reactive and allowing you to use Kotlin Coroutines in an Observer Pattern. It was suggested by some of the Google developers to use LiveData and Kotlin Coroutines over RxJava since LiveData is a smaller library than RxJava and had less overhead. The LiveData vs. RxJava debate is still ongoing, so it’s up to you which you prefer to use. Kotlin Coroutines can work with both.

Kotlin Coroutines in Third Party Libraries

Retrofit supports doing suspend functions. You can use Jake Wharton’s Retrofit Kotlin Coroutines adapter, or use use regular Retrofit with your Kotlin Coroutines. Check out this article for more on Kotlin Coroutines and Retrofit.

Testing with Kotlin Coroutines

Google I/O announced the launch of a new library to help test Kotlin Coroutines, kotlinx-coroutines-test. This library should allow developers to get around having to wait for threads to run by controlling the ‘time’ in a test, handling coroutine scope, and cleaning up state between tests. Apparently it can also be used to test coroutines with Live Data. The test API is still experimental, but hopefully it will be in a stable form soon so we can unit test with it!

Summary

Kotlin Coroutines is a simple, powerful library to handle asynchronous programming with Android. Now that it has first-class Android support and has been integrated with Jetpack, there is even more reason to pick it up! Have fun!

“The Google IO logo, names, images, and all goodwill therein are the property of Google, Inc.”