Home > Mobile >  NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContain
NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContain

Time:07-03

With Jetpack Compose UI Tooling 1.2.0-rc01 and Compose Compiler 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
}

The IDE cannot display the @Preview; it fails with:

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

The actual cause is something else:

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

How can I provide the missing androidx.customview.poolingcontainer.R$id?

CodePudding user response:

As it turned out when reading the sources, this needs the customview-poolingcontainer:

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

Thought it was included in customview, but it's not. This makes the preview behave.


Here it is being explained why it's like that:

PoolingContainer library

This adds an androidx.customview:customview-poolingcontainer artifact that is depended on by both Compose and RecyclerView, through which AbstractComposeView and RecyclerView discover each other and communicate about when the Compose view should dispose its composition.

This mechanism is independent of both Compose and RecyclerView, and could be used for any recycling container or child with heavy resources that should be retained across recycling.

Relnote: "Add a new PoolingContainer library that allows for listening to dispose events of a container that manages its children outside the View hierarchy. This will later be added as a dependency of Compose and RecyclerView"

Bug: 196371929

  • Related