Kotlin Coroutines in Android — Part 1
Introduction
In this series, we’ll explore the stable version of Kotlin coroutines and we’ll see how we can use them to build Android apps.
These are the topics that we’re going to discuss:
- Part 1: Introduction
- Part 2: The basics
- Part 3: Coroutines in Android Studio
- Part 4: Running coroutines sequentially or in parallel
- Part 5: Coroutine cancellation
- Part 6: Exception propagation
- Part 7: A small DSL for Android apps development
- Part 8: MVP and MVVM with Clean Architecture
- Part 9: Combining multiple tasks with the operators on collections
- Part 10: Handling callbacks
- Part 11: Channels
- Part 12: Testing
Let’s start our first part by answering a few frequent questions:
- Why should I care about coroutines?
- I’ve been using RxJava for a long time. What’s the advantage of using coroutines?
- How do I use coroutines to build an Android app?
Why should I care about coroutines?
Kotlin coroutines are a new powerful tool that opens up new opportunities to simplify asynchronous programming.
The main reason to learn about them is to understand how to combine the simplicity of sequential code with the benefits of asynchronous programming. Sequential code is easy to understand, modify and maintain, while asynchronous programming is essential to make your app responsive by offloading many computations to background threads and keep your UI responsive.
I’ve been using RxJava for a long time. What’s the advantage of using coroutines?
There’s always a little bit of resistance in changing a tool that we’ve been using for a very long time. It took us a long time to learn how to use it properly (or at least decently) and there are already many existing patterns that show how to use it. So why should we learn a new one as a replacement?
A reason could be that we simply want to learn something new, while another reason could be that the new tool brings a significant advantage over the previous one and enables us to explore more efficient ways to develop and maintain our software. I believe the latter is the most important reason in this specific case.
So, what are some reasons to switch from RxJava to coroutines?
- RxJava is good to handle streams, but forces us to write everything as if we were processing streams. In the vast majority of cases, we don’t really need to handle any stream in an Android app, so we would like to avoid writing complex chains of operators just to execute simple actions. With coroutines, we have the flexibility of writing sequential code or process streams as we prefer without being forced to just one of those options.
- RxJava forces us to have callbacks at some point to handle the results. This brings complexity to the code and makes it harder to write complex flows. We can combine multiple streams or combine multiple callbacks, but it’s never as simple as sequential code. With coroutines, we can get rid of all the callbacks and our flows immediately become easier to manage and maintain.
If we still need to use RxJava in specific cases, we can combine it with coroutines, so choosing Kotlin coroutines as our main tool for asynchronous programming doesn’t exclude the opportunity of using RxJava when needed.
I guess that the points above might sound a bit abstract right now, but I’m confident that following the examples presented in this series will make everything clear.
How do I use coroutines to build an Android app?
If you’re here, this is probably the main question. We have this new powerful tool, but how can we apply it to our Android apps development?
This series will be specifically about Android. By restricting the scope of our discussion, it will be easier to focus only on the parts that we need as a starting point in the coroutines world. When you’ll be confident with the main topics, it will be easy for you to explore all the additional features on your own.
At the time of writing, Kotlin coroutines version is 1.1.1. This is one of the first stable versions. There are multiple ideas around to apply coroutines for Android development, but there’s also a lot of confusion. It’s expected. We don’t have established patterns yet and this is a good time to figure out some of them.
In this series, we’ll try to provide some of these possible development patterns to take advantage of coroutines specifically for Android apps having an MVP or MVVM architecture by making use of Clean Architecture as well. The choice of restricting to these specific architectures is due to their popularity.
What’s next
This was just a short introduction to understand what you can expect from the Kotlin Coroutines in Android series and the reason behind it. In the next part, we’ll start taking a look at some code with the first building blocks of our coroutines development patterns in Android.
Get the source code
The source code for this series can be found on GitHub. It’s an example Android project that covers multiple cases. Download it and play with it.