kotlin 使用协程后,开启混淆代码,类名字没有被混淆

问题描述

最近做混淆,发现,即使你开了混淆,某些类名称,依旧没有被改变,这部分的代码,又刚刚好是协程写的

问题测试调研

class TestLunch{
    val defaultScope = CoroutineScope(Dispatchers.Default)
    fun helloWorld() {
        defaultScope.launch {
            System.out.println("Hello, world!") 
        }
    }
}
image.png

我们对上面的代码进行反编译,发现会变成静态内部类,且继承了SuspendLambda,SuspendLambda怎么来的?其实我们写协程的时候就是调用了扩展方法

public fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    val newContext = newCoroutineContext(context)
    val coroutine = if (start.isLazy)
        LazyStandaloneCoroutine(newContext, block) else
        StandaloneCoroutine(newContext, active = true)
    coroutine.start(start, coroutine, block)
    return coroutine
}

编译器会帮我们将带有suspend标识的,去继承 SuspendLambda,我们继续去追踪SuspendLambda的父类,结果发现最终的父类 BaseContinuationImpl 去 implements Serializable

SuspendLambda的来源,这篇博客说的很好

image.png

可以看见类名字被保留了下来,后面无意间发现,混淆文件里面有配置

#保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容