抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年
人们总是混淆了欲望和理想

为了 K2 compiler, 将 Kotlin 更新至 2.0.0 需要做的事儿

最近有个开发 IOS 的小需求,想看看 Compose 的 Multiplatform 出新手村没,然后看到评论区一堆人在 IDEA Multiplatform 插件评论区 call 咩有 K2。疑惑 K2 是啥玩意儿,于是在官网看到了”遥遥领先式“宣传手法:

The new K2 compiler is enabled by default starting with 2.0.0. For more information on the new features provided in Kotlin 2.0.0, as well as the new K2 compiler, see What's new in Kotlin 2.0.0.

Performance improvements

To evaluate the performance of the K2 compiler, we ran performance tests on two open-source projects: Anki-Android and Exposed. Here are the key performance improvements that we found:

The K2 compiler brings up to 94% compilation speed gains. For example, in the Anki-Android project, clean build times were reduced from 57.7 seconds in Kotlin 1.9.23 to 29.7 seconds in Kotlin 2.0.0.

The initialization phase is up to 488% faster with the K2 compiler. For example, in the Anki-Android project, the initialization phase for incremental builds was cut from 0.126 seconds in Kotlin 1.9.23 to just 0.022 seconds in Kotlin 2.0.0.

The Kotlin K2 compiler is up to 376% quicker in the analysis phase compared to the previous compiler. For example, in the Anki-Android project, analysis times for incremental builds were slashed from 0.581 seconds in Kotlin 1.9.23 to only 0.122 seconds in Kotlin 2.0.0.

For more details on these improvements and to learn more about how we analyzed the performance of the K2 compiler, see our blog post.

原文:https://kotlinlang.org/docs/k2-compiler-migration-guide.html#performance-improvements

简单说就是:编译速度提升 94%、分析速度提升 376%、初始化阶段速度提升 488%。

文章发布日期也才 4 天前,这次赶了个早,从标题看,主要是利好 Multiplatform ,不过无所谓,需要 Kotlin 2.0.0,也算专业对口,这就给自己的 APP 安排。

如果经常用 Android studio 自动更新的朋友,一定会发现触发编译必有错误,当你费尽心思调顺后,warning 又会将你指向这俩文档,意思要用 2.0.0 必须整这么个 compiler :

https://developer.android.com/develop/ui/compose/compiler?hl=zh-cn

https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-compiler.html

但不好意思,一路按他的流程 next 下来,只会是 Error 、Sync failed 等常见组合拳,我习惯称为“水土不服”。

总的来说,就是他们在做 kotlin、gradle、compose 的分分合合,没说的那么简单,也没那么复杂。

Plugin 的分分合合

一句话:增加 compose-compiler-gradle-plugin ,移除 kotlin-gradle-plugin 换为 kotlin.android.gradle.plugin

对于 plugin 的管理,可能一些老旧项目切不过来,这里分两个版本。

非 TOML 管理版本

在用 classpath 的那个根 build.gradle.kts 追加一个 compose-compiler-gradle-plugin ,移除 kotlin-gradle-plugin,换为 kotlin.android.gradle.plugin,同时需要更新下 gradle 版本 ,完整版:

classpath("com.android.tools.build:gradle:8.9.2")
//kotlin 2.0+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.0")
classpath("org.jetbrains.kotlin.android:org.jetbrains.kotlin.android.gradle.plugin:2.0.0")
classpath("org.jetbrains.kotlin:compose-compiler-gradle-plugin:2.0.0")

TOML 管理的版本

这里就不多说了,根 build.gradle.kts ,移除 kotlin-gradle-plugin ,换为 kotlin.android

plugins {
alias(libs.plugins.org.jetbrains.kotlin.android) apply false
alias(libs.plugins.compose.compiler.plugin) apply false
}

libs.versions.toml 文件:

[versions]
kotlin = "2.0.0"

[plugins]
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler-plugin = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

然后

既然增加了一个 plugin,自然在 module 也需要配置,比如 build.gradle (若是 kts 自行按 kts 写法):

plugins {
...
id 'org.jetbrains.kotlin.plugin.compose'
}

不多吧,就这点东西,可以成功同步了。这段代码记得删了:

composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}

官方文档提到的 Compose 编译器 Gradle 插件配置选项,也可以加了:

android { … }

composeCompiler {
    reportsDestination = layout.buildDirectory.dir("compose_compiler")
    stabilityConfigurationFile = rootProject.layout.projectDirectory.file("stability_config.conf")
}

PS: Compose 稳定性的配置文件 stability_config.conf 还请手动创建,这部分的配置见:

https://developer.android.com/develop/ui/compose/performance/stability/fix?hl=zh-cn#configuration-file

Kotlin 官方推荐的注解处理工具:KSP

按上面的流程,可以编译了,但编译一下就会发现:

w: Kapt currently doesn't support language version 2.0+. Falling back to 1.9.

Kapt 仅支持 Kotlin 1.9,且进入了维护阶段,而有个新东西,com.google.devtools.ksp 已经先行了很久,于是

同样在 classpath 的那个根 build.gradle.kts 增加(toml 的更简单就不贴了):

//replace kapt
classpath("com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:2.0.0-1.0.24")

在 module 中的 build.gradle 更换 ‘kotlin-kapt’ 为:

plugins {
// byby kapt
id 'com.google.devtools.ksp'
}

然后移除所有的 kapt 配置项,将 dependencies 中的 kapt 替换为 ksp。Kotlin 是 2.0.0 ,Ksp 则也只能是 2.0.0,否则会甩你一堆“版本太高了”,具体版本见:

https://central.sonatype.com/artifact/com.google.devtools.ksp/com.google.devtools.ksp.gradle.plugin/versions

不过也就需要一直忍受 A newer version of com. google. devtools. ksp than 2.0.0-1.0.24 is available,但不好意思就是不能升。

无了

上面就是 update to kotlin 2.0.0 需要干的简单事儿了。

试用了一把,本来就是 M4 ,编译实在是没什么太多感受,以前 Intel 转 2/3min 的东西,现在不过 25s/1s ,硬件决定的还是多一点

不过别失望,折腾是值得的,Kotlin 2.0.0 随之而来的 Compose 生成的 Debug 应用流畅了许多。

后续

既然是大更新,自然免不了要适配,目前初步测试,触摸/动画事件的改变较多,已知:

1、awaitHorizontalTouchSlopOrCancellation 行为进行了变更,事件消费后其他控件依旧可消费,不如直接使用 detectHorizontalDragGestures 可靠,像是刻意的规正,扩展进一步收缩。不过也许是BUG,毕竟 Kotlin 1.9 并没问题。

2、API 变更,这个官网有披露,animateItemPlacement() 变成了 animateItem()。

既然要适配,于是后来一口气直接更新到最新的 2.1.20,Debug 版 APP 流畅度居然够得着正式版,相当的离谱!!!当然敢这么改,也是自 TOML 管理后,只需要改改 TOML 文件的 Version 不用到处找代码来的方便。

建议仅 try try dev,毕竟大项目免不了各种自定义事件,一个个调未免太费时。

以上。


本站广告由 Google AdSense 提供

0条评论

发表评论

在 TA 离去的那一刻

“仍在努力工作”