无法再查看 Jetpack Compose 预览。无法实例化一个或多个 类 (ComposeViewAdapter)
Can no longer view Jetpack Compose Previews. Failed to instantiate one or more classes (ComposeViewAdapter)
我最近遇到一个错误,阻止任何 Jetpack Compose 预览显示如下:
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(RenderTask.java:895)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.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)
我尝试过清除缓存、重建我的项目、按照建议清理我的项目,但没有任何帮助。我在 Android Studio Bumblebee 2021.2.1 补丁 3 和 Chipmunk 2021.2.1 beta 4 上都遇到了同样的问题。
我目前对所有与 Compose 相关的依赖项使用 1.1.1 版,但已尝试降级并使用较新的版本,但没有任何帮助。
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'
我认为这里有几个问题询问类似的事情,但它们都很老,并且使用的是早已过时的 Compose 和 Android Studio 版本,因此它们不是很有帮助。
这是一个已知错误:https://issuetracker.google.com/issues/227767363
Google 目前正在修复,但已经有解决方法:将这些依赖项添加到使用 Compose 预览的每个模块:
debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
如果你有一个通用的 core/base/ui 模块,你可以将它添加到那里并使用 Api
而不是 Implementation
以避免将它添加到每个模块:
debugApi "androidx.customview:customview:1.2.0-alpha01"
debugApi "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
神器发布reference
我正在将我的一个简单项目迁移到 compose,我遇到了这个问题。然后我意识到我忘了添加 "androidx.compose.ui:ui-tooling:1.1.1"
这些是我对 jetpack compose 的所有依赖。添加最后一行就可以了。花了 20 分钟才意识到这一点...
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")
debugImplementation("androidx.compose.ui:ui-tooling:1.1.1")
我最近遇到一个错误,阻止任何 Jetpack Compose 预览显示如下:
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(RenderTask.java:895)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.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)
我尝试过清除缓存、重建我的项目、按照建议清理我的项目,但没有任何帮助。我在 Android Studio Bumblebee 2021.2.1 补丁 3 和 Chipmunk 2021.2.1 beta 4 上都遇到了同样的问题。
我目前对所有与 Compose 相关的依赖项使用 1.1.1 版,但已尝试降级并使用较新的版本,但没有任何帮助。
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'
我认为这里有几个问题询问类似的事情,但它们都很老,并且使用的是早已过时的 Compose 和 Android Studio 版本,因此它们不是很有帮助。
这是一个已知错误:https://issuetracker.google.com/issues/227767363
Google 目前正在修复,但已经有解决方法:将这些依赖项添加到使用 Compose 预览的每个模块:
debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
如果你有一个通用的 core/base/ui 模块,你可以将它添加到那里并使用 Api
而不是 Implementation
以避免将它添加到每个模块:
debugApi "androidx.customview:customview:1.2.0-alpha01"
debugApi "androidx.customview:customview-poolingcontainer:1.0.0-alpha01"
神器发布reference
我正在将我的一个简单项目迁移到 compose,我遇到了这个问题。然后我意识到我忘了添加 "androidx.compose.ui:ui-tooling:1.1.1"
这些是我对 jetpack compose 的所有依赖。添加最后一行就可以了。花了 20 分钟才意识到这一点...
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")
debugImplementation("androidx.compose.ui:ui-tooling:1.1.1")