Android Compose 升级 1.2.1 记录
之前一直使用 Compose 1.1.1 版本,升级至 1.2.1 后就开始报错了。
1)compose.compiler 找不到 1.2.1 版本
Compose 版本和 Kotlin 版本是对应的,具体的版本对应可参考 Compose-Kotlin compatibility map
Compose 1.2.0 之后,compose 和 kotlin 版本不再是强依赖关系了,具体可以阅读:https://android-developers.googleblog.com/2022/06/independent-versioning-of-Jetpack-Compose-libraries.html
Starting today, the various Jetpack Compose libraries will move to independent versioning schemes. This creates the possibility for sub-groups such as androidx.compose.compiler or androidx.compose.animation to follow their own release cycles.
Allowing these libraries to be versioned independently will decouple dependencies which were previously implicitly coupled, thereby making it easier to incrementally upgrade your application and therefore stay up-to-date with the latest Compose features.
...
Compose and Kotlin are highly coupled, and we’ve heard your feedback that Compose compiler updates are needed to allow you to upgrade your Kotlin version. We want to make sure that you can use the latest and greatest features (and bug fixes) from both Compose and Kotlin, which is why we plan to release stable versions of the Compose Compiler on a much more regular basis. This means the Compose Compiler version numbers will progress at a faster pace than most other Compose libraries. Since the Compose Compiler is both forwards and backwards compatible, you will be able to upgrade it as soon as a new version is released.
大意是说,各 Compose 库将转为独立的版本管理方案,允许 Compose 库独立发版,使你的 App 更容易增量升级,保持与最新的 Compose 功能同步。第一个独立版本管理的库是 Compose Compiler,由于这个库是向前和向后兼容的,一旦有新的版本发布,你就可以立即升级。Compose Compiler 和 Kotlin 版本是强依赖关系。
这就清楚了,Compose Compiler 已经是独立版本管理,需要单独选择版本,其最新版本为 1.3.0 :https://developer.android.com/jetpack/androidx/releases/compose-compiler
compose-compiler-release.pngcompose-compiler-kotlin.png
修改 build.gradle:
// project build.gradle
ext {
...
kotlin_version = '1.7.10'
compose_version = '1.2.1'
compose_compiler_version = '1.3.0'
}
// app build.gradle
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_version
}
2)AbstractMethodError
改完 build.gradle 开开心心 run 一把,结果发现新错误:
java.lang.AbstractMethodError: abstract method "void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)"
这个是已知错误,在低版本 Android 上出现,参考:https://developer.android.com/jetpack/androidx/releases/compose-runtime
compose-AbstractMethodError.png解决起来也简单,只需要把 minSdkVersion 升级到 24+ 就可以了,😂 那岂不是最低支持 Android 7.0 ……
只能换姿势了,不能使用 DefaultLifecycleObserver,我还可以使用 LifecycleEventObserver 嘛:
class XxxLifeCycleObserver : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_RESUME -> {
// sth todo
}
Lifecycle.Event.ON_PAUSE -> {
// sth todo
}
Lifecycle.Event.ON_DESTROY -> {
// sth todo
}
else -> {}
}
}
}
3)总结
Compose 版本升级结束,建议大家也升级到新版本。animation,foundation,runtime 和 ui 等几个库的官方描述是 Important changes since 1.1.0,详细信息请阅读:https://developer.android.com/jetpack/androidx/releases/compose