从 Android Studio 运行 Android 应用程序时如何启用协程调试模式? [英] How to enable coroutine debug mode when running an Android App from Android Studio?

查看:124
本文介绍了从 Android Studio 运行 Android 应用程序时如何启用协程调试模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过 Android Studio 在调试模式下运行我的 Android 应用时遇到协程异常.

I'm getting a coroutine exception while running my Android app in debug mode through Android Studio.

kotlinx.coroutines.JobCancellationException:StandaloneCoroutine 被取消

kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled

来自 协程调试文档,我认为通过启用协程的调试模式,我可以获得更完整的堆栈跟踪信息.

From the coroutines debugging documentation, I gather that I might get fuller stack trace information by enabling debug mode of coroutines.

可以通过设置系统属性 DEBUG_PROPERTY_NAME 或通过启用断言(-ea 标志)运行 Java 来启用它.

It can be enabled either by setting system property DEBUG_PROPERTY_NAME or by running Java with enabled assertions (-ea flag).

这就是我被卡住的地方.在 Android Studio 中实现这一目标的惯用方法是什么?我的项目是使用 Gradle 配置的,我在 Windows 上运行.理想情况下,有一种方法可以通过 Gradle 配置进行配置,以便任何人通过源代码控制引入该项目时都可以启用协程调试模式.

This is where I'm stuck. What is the idiomatic way of achieving this in Android Studio? My project is configured using Gradle, and I am running on Windows. Ideally, there is a way to configure this through Gradle configuration so that coroutines debug mode is enabled for anyone pulling in this project through source control.

推荐答案

我还没有找到通过 Android Studio 或 Gradle 进行配置的方法.这样做的信息对我仍然有用.但是,以下经过验证可以正常工作;我得到了完整的堆栈跟踪.

I haven't found a way to configure this through Android studio or Gradle. Information on doing so would still be useful to me. But, the following is verified to work; I got a full stack trace.

系统属性"指的是 Java 系统属性.它们可以在运行时使用 System.setProperty 设置.

The "system property" refers to Java System Properties. They can be set at runtime using System.setProperty.

因此,我将以下代码添加到我的 Application.onCreate() 的开头.

I therefore added the following code to the start of my Application.onCreate().

override fun onCreate() {
    // Enable coroutines debug mode in debug builds.
    if (BuildConfig.DEBUG) {
        System.setProperty(
            kotlinx.coroutines.DEBUG_PROPERTY_NAME,
            kotlinx.coroutines.DEBUG_PROPERTY_VALUE_ON
        )
    }

    ...

}

这篇关于从 Android Studio 运行 Android 应用程序时如何启用协程调试模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆