Shift-left benefits for mobile app development is not just a tool problem

AsyncTask is an Android API that handles asynchronous jobs. To put this another way, AsyncTask provides a mechanism for handling background jobs.
Some processes in an app might be too heavy or involve waiting for responses. It’s not recommended to execute such processes on the main UI thread as it can cause the UI thread to pause until a response is returned. And that’s where AsyncTask comes in with its doInBackground() method.
In this post, I’ll answer if you should still use AsyncTask in 2021. In addition, I’ll discuss how the AsyncTask API works. We’ll also take a look at the pros and cons of AsyncTask and some alternatives.
Let’s start by explaining how the AsyncTask API works.
AsyncTask is an Android-specific API that makes working on the UI easier. Three generic types define an AsyncTask. The three generics are listed below:
You may set any of the generics to Void. And that would mean no value will be provided or returned.
AsyncTask gets heavy or delayed jobs off the UI thread and updates Result or Progress back to the UI. There are four steps for doing this. The steps are as follows:
The following is a code sample for Kotlin AsyncTask. The code implements the doInBackground() and onPostExecute() methods.
class doAsync() : AsyncTask<Void, Void, String>() {
override fun doInBackground(vararg params: Void?): String? {
return "Yeah!"
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
if (result != null) {
Log.d("AsyncTest", result)
}
}
}
The above code simply returns the string “Yeah!” as a result from the background task. However, in a real application, the result may be the response from a network request. Then, the result is printed out to the Logcat in the post execute step.
Now that we have a good understanding of AsyncTask, let’s see some pros and cons.
AsyncTask is now a member of the infamous deprecated club. Hence, we should forget using it in 2021. However, below are some pros of AsyncTask that might make anyone still consider using it.
AsyncTask is deprecated in Android API 30. In other words, official support for the API has ended. Therefore, the Android team does not recommend AsyncTask. This on its own is a disadvantage. In this section, we’ll look at some of the reasons why you wouldn’t want to use AsyncTask.
Comparing the pros and cons, I’d say no. You should not use AsyncTask in 2021. This is because the disadvantages are greater than any advantage of doing so. In addition, best practice recommends the removal of any deprecated code from your application. As a result, you should not add new AsyncTask code to your project. Also, you should consider updating old code that still uses AsyncTask.
In the next section, we’ll take a look at some alternatives for AsyncTask.
Today, we have many ways for handling asynchronous tasks in Android.
If you would prefer to use a solution that doesn’t require a third-party library, the official documentation for AsyncTask recommends Executors from java.util.concurrent.
Another alternative is Kotlin coroutines. This is an API for handling asynchronous tasks in Kotlin. Unlike AsyncTask, coroutine is life cycle aware. Therefore, there are fewer chances for memory leaks. This option is popular amongst Kotlin developers. Hence, it’s easy to learn as there are many resources out there.
Coroutines are highly testable. And there is an entire library dedicated to testing coroutines. The Kotlin coroutines test library makes writing unit tests for coroutines easy. Unit tests verify the behavior of a unit of your code—usually, a single function or method.
Lastly, Waldo offers a no-code testing solution for mobile engineers. With Waldo, it’s possible to test the results from an asynchronous task without any extra setup or code. You can try Waldo for free here.
To conclude, we’ll summarize everything we’ve been able to cover in this post.
We started by learning about what AsyncTask is. And we defined AsyncTask as an API for doing heavy tasks off the UI thread. Next, we looked at some pros and cons of AsyncTask.
Finally, we answered our main question of “Should I still use AsyncTask in 2021?” From all the facts available, the answer to the question is a simple no. Hence, don’t use AsyncTask for new projects. Consider using one of the alternatives we mentioned instead. The alternatives prevent memory leaks and have official support. For example, coroutines are recommended for doing asynchronous tasks in Kotlin.
This post was written by Pius Aboyi. Pius is a mobile and web developer with over 4 years of experience building for the Android platform. He writes code in Java, Kotlin, and PHP. He loves writing about tech and creating how-to tutorials for developers.