I've recently got an error preventing any Jetpack Compose previews from being displayed as follows:
Failed to instantiate one or more classes
The following classes could not be instantiated:
-androidx.compose.ui.tooling.ComposeViewAdapter(Open Class, Show Exception, Clear Cache)
if this is an unexpected error you can also try to build the project, then manually refresh the layout
java.lang.NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContainer
at androidx.compose.ui.platform.ViewCompositionStrategy$DisposeOnDetachedFromWindowIfNotInPoolingContainer.installFor(ViewCompositionStrategy.android.kt:97)
at androidx.compose.ui.platform.AbstractComposeView.<init>(ComposeView.android.kt:123)
at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:392)
at androidx.compose.ui.platform.ComposeView.<init>(ComposeView.android.kt:388)
at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:131)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:339)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:176)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:136)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:301)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:505)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:363)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:436)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:121)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:739)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:895)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:187)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
I have tried clearing the cache, rebuilding my project, cleaning my project as suggested but nothing has helped. I have had the same issue on both Android Studio Bumblebee 2021.2.1 Patch 3 as well as Chipmunk 2021.2.1 beta 4.
I am currently using version 1.1.1 for all my Compose related dependencies, but have tried downgrading and using newer versions but nothing helps.
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:1.1.1"
implementation "androidx.compose.material:material:1.1.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.1.1"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.1.1'
I think there are a couple of questions on here asking similar things, but they are all very old and use long outdated versions of Compose and Android Studio, so they are not very helpful.
It is a known bug: https://issuetracker.google.com/issues/227767363
Google is currently working on a fix but there is already a workaround: add these dependencies to every module where you use the Compose preview:
debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
If you have a common core/base/ui module, you can add it there and use Api
instead of Implementation
to avoid adding it to every module:
debugApi "androidx.customview:customview:1.2.0-alpha01"
debugApi "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
Artifacts release reference