NoClassDefFoundError:无法初始化类androidx.customview.poolingcontai



使用Jetpack撰写UI工具1.2.0-rc01和撰写编译器1.2.0;

android {
composeOptions {
kotlinCompilerExtensionVersion "1.2.0"
}
}
dependencies {
debugImplementation "androidx.compose.ui:ui-tooling:1.2.0-rc01"
debugImplementation "androidx.customview:customview:1.1.0" // being pulled in by another dependency
debugImplementation "androidx.customview:customview:1.2.0-alpha01" // also tried the latest version
}

IDE无法显示@Preview;失败:

java.lang.NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContainer
at androidx.compose.ui.platform.ViewCompositionStrategy$DisposeOnDetachedFromWindowOrReleasedFromPool.installFor(ViewCompositionStrategy.android.kt:97)
...

真正的原因是别的:

java.lang.ClassNotFoundException: androidx.customview.poolingcontainer.R$id
at com.android.tools.idea.rendering.classloading.loaders.DelegatingClassLoader.findClass(DelegatingClassLoader.kt:81)
...

如何提供缺失的androidx.customview.poolingcontainer.R$id?

在阅读源文件时发现,这需要customview-poolingcontainer:

debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0"

原以为它包含在customview中,但它没有。这使得预览正常。


这里解释了为什么会这样:

PoolingContainer图书馆

这添加了ComposeRecyclerView都依赖的androidx.customview:customview-poolingcontainer工件,通过AbstractComposeViewRecyclerView相互发现并就Compose视图何时应该处理其组合进行通信。

此机制独立于ComposeRecyclerView,并且可用于任何回收容器或重资源的儿童这应该在回收过程中保留下来。

Relnote: "添加一个新的允许侦听的PoolingContainer库处置在视图外部管理其子容器的事件层次结构。稍后将作为ComposeRecyclerView">

Bug: 196371929

最新更新