最近学习Kotlin Coroutines的时候,在Debugging coroutines and threads中看到可以通过设置JVM Option开启coroutine debug,在IDEA中可以很容易的在工程设置里找到该选项,但是在Android Studio中就没那么方便了。找了好久也没找到在哪里设置,后来,有大佬提示,可以通过下面两种方式来开启:
1. 通过代码设置
在自己的Application类的onCreate里添加下面配置就可以了
System.setProperty("kotlinx.coroutines.debug", "on")
2. 开启R8再配置
在gradle.properties里开启R8
android.enableR8 = true
然后在proguard-rules.pro文件中添加以下配置:
-assumevalues class kotlinx.coroutines.Debugkt {
static boolean DEBUG return true;
}
通过上面两种方式配置好后,再通过
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
就能打印出协程名称来了:
System.out: [kotlinx.coroutines.DefaultExecutor @coroutine#2] 5. Hello
或许还有其他方便的配置,知道的道友还请指点一下