I started having a weird problem since yesterday. Basically, none of my previews can be rendered, and if I click on the small info icon close to the preview, I see the following stacktrace:
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)
Now, I'm pretty sure I haven't changed any of my build files for at least a week (I checked Git history), so it can't be due to some misconfigured dependencies. I haven't updated Android Studio or any SDK-related tools either.
I'm using Android Studio Giraffe (2022.3.1
), with Jetpack Compose BOM set to '2023.06.01'
(as I said previously, none of these versions were changed since yesterday)
Cleaning and rebuilding the project didn't help; invalidating the project's cache doesn't seem to work either. What could it be, how could this class simply disappear from the classpath?
The solution turned out to be so simple that I was banging my head against the wall for a couple of minutes.
You can't simply render the Compose Preview with a set of runtime libraries; to do that, you need to have one specific library that provides extra code needed by the IDE. This is how this library is usually defined:
debugImplementation "androidx.compose.ui:ui-tooling"
(The version code is omitted because, as I said previously, I rely on Jetpack Compose BOM to define all JC versions).
Now, yesterday I was tracking down the error that seemed to be only reproduced on release builds, so in order to do that, I had to switch to release builds. Once I did that, ui-tooling
was no longer available for Android Studio, so, naturally, that class that was mentioned in the stacktrace, disappeared.
To fix that issue, open the "Build Variants" menu in the left corner, and make sure that your build variant is set to debug
/ productionDebug
or whatever build variant you have in your project. Setting it to something that has debug
in its name is the core thing.
That's it.