之前一直使用 Compose 1.1.1 版本,升級(jí)至 1.2.1 后就開始報(bào)錯(cuò)了。
1)compose.compiler 找不到 1.2.1 版本
Compose 版本和 Kotlin 版本是對(duì)應(yīng)的,具體的版本對(duì)應(yīng)可參考 Compose-Kotlin compatibility map
Compose 1.2.0 之后,compose 和 kotlin 版本不再是強(qiáng)依賴關(guān)系了,具體可以閱讀: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 庫(kù)將轉(zhuǎn)為獨(dú)立的版本管理方案,允許 Compose 庫(kù)獨(dú)立發(fā)版,使你的 App 更容易增量升級(jí),保持與最新的 Compose 功能同步。第一個(gè)獨(dú)立版本管理的庫(kù)是 Compose Compiler,由于這個(gè)庫(kù)是向前和向后兼容的,一旦有新的版本發(fā)布,你就可以立即升級(jí)。Compose Compiler 和 Kotlin 版本是強(qiáng)依賴關(guān)系。
這就清楚了,Compose Compiler 已經(jīng)是獨(dú)立版本管理,需要單獨(dú)選擇版本,其最新版本為 1.3.0 :https://developer.android.com/jetpack/androidx/releases/compose-compiler


修改 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 一把,結(jié)果發(fā)現(xiàn)新錯(cuò)誤:
java.lang.AbstractMethodError: abstract method "void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)"
這個(gè)是已知錯(cuò)誤,在低版本 Android 上出現(xiàn),參考:https://developer.android.com/jetpack/androidx/releases/compose-runtime

解決起來也簡(jiǎn)單,只需要把 minSdkVersion 升級(jí)到 24+ 就可以了,?? 那豈不是最低支持 Android 7.0 ……
只能換姿勢(shì)了,不能使用 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)總結(jié)
Compose 版本升級(jí)結(jié)束,建議大家也升級(jí)到新版本。animation,foundation,runtime 和 ui 等幾個(gè)庫(kù)的官方描述是 Important changes since 1.1.0,詳細(xì)信息請(qǐng)閱讀:https://developer.android.com/jetpack/androidx/releases/compose