- Add kotlinx-coroutines-android module as dependency when using kotlinx.coroutines on Android:
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'
- version
code text
ext.kotlin_version = '1.3.70'
- Android Studio中Coroutine Debug 两种方式
- 通过代码设置, 在自己的Application类的onCreate里添加下面配置就可以了
System.setProperty("kotlinx.coroutines.debug", "on")
- 开启R8再配置
//在gradle.properties里开启R8
android.enableR8 = true
然后在proguard-rules.pro文件中添加以下配置:
-assumevalues class kotlinx.coroutines.Debugkt {
static boolean DEBUG return true;
}
- Demo code
import kotlinx.coroutines.*
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
fun main() = runBlocking<Unit> {
val a = async {
log("I'm computing a piece of the answer")
6
}
val b = async {
log("I'm computing another piece of the answer")
7
}
log("The answer is ${a.await() * b.await()}")
}
- 执行结果:
2020-05-14 17:56:28.627 10024-10024/com.yy.kotlindemo I/System.out: [main @coroutine#2] I'm computing a piece of the answer
2020-05-14 17:56:28.628 10024-10024/com.yy.kotlindemo I/System.out: [main @coroutine#3] I'm computing another piece of the answer
2020-05-14 17:56:28.628 10024-10024/com.yy.kotlindemo I/System.out: [main @coroutine#1] The answer is 42
网友评论