Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Kotlins coroutines in Android

Is it possible to debug a coroutine in Android? When I tried to debug this code snippet:

runBlocking {
        try {
            async(CommonPool) {
                showLoadingIndicator()
                val a = loadData().await()
                hideLoadingIndicator()
            }
        } catch (e: Exception) {
            Log.e("lala", "exception " + e.toString())
        }
}

val a has the following message: Cannot find local variable: name = a

In my App, I'm setting the property for coroutines debugging as followed:

System.setProperty("kotlinx.coroutines.debug", if (BuildConfig.DEBUG) "on" else "off")

Still not working. Now I have no idea how to use the debugger when working with coroutines. May you guys help me please?

Thanks in advance

Alban

like image 349
NullPointer Avatar asked Jan 31 '26 12:01

NullPointer


1 Answers

I tend not to add debug point at the suspend point as that is where coroutine changes the code. Instead, add a debugger at some lambda maybe inside coroutineScope, or inside the body of a suspend fun.

Also you can add Log.d("CoroutineDebug", "your message") in android or println in Java project to get some info about your code execution.

BTW mentioned, issues https://youtrack.jetbrains.com/issue/KT-22296 and https://youtrack.jetbrains.com/issue/KT-25076 are already closed. So you will be able to evaluate the variables easily while debugging

like image 102
AndroidEngineX Avatar answered Feb 02 '26 02:02

AndroidEngineX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!