Kotlin,Android,如何正确调试协程?
Kotlin, Android, how to debug coroutines correctly?
我正在尝试调试我的协程,但放置在挂起函数中的断点不起作用。请帮我理解为什么。
使用 Android Studio。
好的,我从 viewModelScope 启动协程:
viewModelScope.launch(IO) {
when(val result = interactor.getAllWords()){...}
}
在getAllWords()
中我写道:
override suspend fun getAllWords(): WordResult {
val words = mutableListOf<Word>()
when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}
...
return getWordsWithSelectedPattern()
我有两个挂起函数:getAllWordsWithoutFiltersApplying()
和 getWordsWithSelectedPattern()
。我在它们中都设置了断点,但它们没有在调试模式下触发。
同时,val words = mutableListOf<Word>()
行被触发,当我把断点放在它的行上时。
而且,如果我将一些日志内容放入“untracing”功能中,它们就会起作用。我这样说是为了清楚,暂停功能有效。断点不是。
我应该如何调试它们?
*已添加屏幕截图。查看左侧有一排图标。为什么我的线路不可用?
我知道我迟到了,但这是为那些和我一样犯过类似错误的人准备的。我最近也遇到了这个问题,根本原因与依赖关系有关。实际上我添加了 coroutine core dependency 但我忘记添加 coroutine android dependency。请确保这两个依赖项都存在于您的 gradle 文件中,如下所示。
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
根据您的示例代码,您在 MAIN
和 IO
之间切换协程上下文,因此在设置断点时,请确保 suspend
选项为 ALL
显示断点的选项。点击鼠标左键设置断点,然后在断点上点击鼠标右键
如果你使用的是JetBrainIDE,根据document,设置断点时确保suspend
选项是ALL
不是线程。它对我有用。
更多细节你可以查看document
它仍然对我不起作用,因为我先 运行 应用程序然后附加调试器,但是当我改用调试器时,它起作用了。
我忘记了在我的 build.gradle
文件
中将 minifyEnabled
设置为 true
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled true <- Should be false for these breakpoints to work
}
}
我正在尝试调试我的协程,但放置在挂起函数中的断点不起作用。请帮我理解为什么。
使用 Android Studio。
好的,我从 viewModelScope 启动协程:
viewModelScope.launch(IO) {
when(val result = interactor.getAllWords()){...}
}
在getAllWords()
中我写道:
override suspend fun getAllWords(): WordResult {
val words = mutableListOf<Word>()
when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}
...
return getWordsWithSelectedPattern()
我有两个挂起函数:getAllWordsWithoutFiltersApplying()
和 getWordsWithSelectedPattern()
。我在它们中都设置了断点,但它们没有在调试模式下触发。
同时,val words = mutableListOf<Word>()
行被触发,当我把断点放在它的行上时。
而且,如果我将一些日志内容放入“untracing”功能中,它们就会起作用。我这样说是为了清楚,暂停功能有效。断点不是。
我应该如何调试它们?
*已添加屏幕截图。查看左侧有一排图标。为什么我的线路不可用?
我知道我迟到了,但这是为那些和我一样犯过类似错误的人准备的。我最近也遇到了这个问题,根本原因与依赖关系有关。实际上我添加了 coroutine core dependency 但我忘记添加 coroutine android dependency。请确保这两个依赖项都存在于您的 gradle 文件中,如下所示。
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
根据您的示例代码,您在 MAIN
和 IO
之间切换协程上下文,因此在设置断点时,请确保 suspend
选项为 ALL
显示断点的选项。点击鼠标左键设置断点,然后在断点上点击鼠标右键
如果你使用的是JetBrainIDE,根据document,设置断点时确保suspend
选项是ALL
不是线程。它对我有用。
更多细节你可以查看document
它仍然对我不起作用,因为我先 运行 应用程序然后附加调试器,但是当我改用调试器时,它起作用了。
我忘记了在我的 build.gradle
文件
minifyEnabled
设置为 true
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled true <- Should be false for these breakpoints to work
}
}