之前一直使用 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 版本不再是強依賴關(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 庫將轉(zhuǎn)為獨立的版本管理方案,允許 Compose 庫獨立發(fā)版肢专,使你的 App 更容易增量升級舞肆,保持與最新的 Compose 功能同步。第一個獨立版本管理的庫是 Compose Compiler博杖,由于這個庫是向前和向后兼容的椿胯,一旦有新的版本發(fā)布,你就可以立即升級剃根。Compose Compiler 和 Kotlin 版本是強依賴關(guān)系哩盲。
這就清楚了,Compose Compiler 已經(jīng)是獨立版本管理,需要單獨選擇版本廉油,其最新版本為 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)新錯誤:
java.lang.AbstractMethodError: abstract method "void androidx.lifecycle.DefaultLifecycleObserver.onCreate(androidx.lifecycle.LifecycleOwner)"
這個是已知錯誤,在低版本 Android 上出現(xiàn)抒线,參考:https://developer.android.com/jetpack/androidx/releases/compose-runtime
解決起來也簡單班巩,只需要把 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)總結(jié)
Compose 版本升級結(jié)束,建議大家也升級到新版本眨猎。animation抑进,foundation,runtime 和 ui 等幾個庫的官方描述是 Important changes since 1.1.0睡陪,詳細信息請閱讀:https://developer.android.com/jetpack/androidx/releases/compose