Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin, Android, how to debug coroutines correctly?

I'm trying to debug my coroutines, and breakpoints placed into suspend function don't work. Pls help me understand why.

Working with Android Studio.

Ok, I launch a coroutine from viewModelScope:

    viewModelScope.launch(IO) {
        when(val result = interactor.getAllWords()){...}
    }

In getAllWords() I wrote:

    override suspend fun getAllWords(): WordResult {
        val words = mutableListOf<Word>()

        when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}

        ...

        return getWordsWithSelectedPattern()

I have two suspend functions: getAllWordsWithoutFiltersApplying() and getWordsWithSelectedPattern(). I have a breakpoints into both of them, but they did't trigger in debug mode.

At the same time, line val words = mutableListOf<Word>() is triggering, when I put breakpoint to its line.

And, if I put some log stuff into "untracing" function, they will be work. I say it to make it clear, suspend function works. Breakpoints are not.

What should I do to debug them?

*Screenshot added. Look at the left side with row of icons. Why my lines are not available?

enter image description here

like image 847
KirstenLy Avatar asked Dec 15 '19 14:12

KirstenLy


People also ask

Do Kotlin coroutines run in parallel?

Android Online Course for Professionals by MindOrks Similarly, we can do any type of background tasks in parallel using Kotlin Coroutines.

Do coroutines run in parallel?

Running coroutines sequentially or in parallel One of the main advantages of asynchronous programming is that it allows to offload computations to separate threads and run them in parallel in case the result of each task is not needed by the others. They can all run independently making use of multiple CPU cores.

What is GlobalScope in Kotlin coroutines?

Quoting definition of Global Scope from Kotlin's documentation– “Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely.” GlobalScope creates global coroutines and these coroutines are not children of some specific scope.


4 Answers

Based on your sample code, you switch the coroutine context between MAIN and IO so when you set the breakpoint, make sure the suspend option is ALL

To show the option of the breakpoint. Set a breakpoint with the left click of your mouse, and then right click your mouse on the breakpoint.

If you are using the JetBrain IDE, according to the document, when you set the breakpoint to make sure the suspend option is ALL not thread. it works for me.

enter image description here

and more detail you can check the document

enter image description here

like image 133
Jeffery Ma Avatar answered Nov 05 '22 02:11

Jeffery Ma


It still does not work for me because I run the app first then attach the debugger, but when I use debug instead, it works.

like image 38
mohsen zandie Avatar answered Nov 05 '22 04:11

mohsen zandie


I know I'm late, but this is for those people who made similar mistakes like me. I've also faced this issue recently and the root cause was related to dependencies. Actually I added coroutine core dependency but I forget to add coroutine android dependency. Please make sure both dependencies are present in your gradle file as shown below.

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
like image 1
Arun P M Avatar answered Nov 05 '22 03:11

Arun P M


I had forgot minifyEnabled set to true in my build.gradle file

  buildTypes {
    release {
      minifyEnabled true
    }
    debug {
      minifyEnabled true <- Should be false for these breakpoints to work
    }
  }
like image 1
nilsi Avatar answered Nov 05 '22 02:11

nilsi