'Android Studio compose preview not working
I can't preview any composables with Android Studio Arctic Fox Beta 4. Even the default one that is generated with an empty compose project. Running the default app on a hardware device doesn't work either Here is the stack trace:
java.lang.NoSuchMethodError: 'androidx.compose.ui.text.TextStyle androidx.compose.ui.text.TextStyle.copy-H99Ercs$default(androidx.compose.ui.text.TextStyle, long, long, androidx.compose.ui.text.font.FontWeight, androidx.compose.ui.text.font.FontStyle, androidx.compose.ui.text.font.FontSynthesis, androidx.compose.ui.text.font.FontFamily, java.lang.String, long, androidx.compose.ui.text.style.BaselineShift, androidx.compose.ui.text.style.TextGeometricTransform, androidx.compose.ui.text.intl.LocaleList, long, androidx.compose.ui.text.style.TextDecoration, androidx.compose.ui.graphics.Shadow, androidx.compose.ui.text.style.TextAlign, androidx.compose.ui.text.style.TextDirection, long, androidx.compose.ui.text.style.TextIndent, int, java.lang.Object)'
at androidx.compose.material.TypographyKt.withDefaultFontFamily(Typography.kt:284)
at androidx.compose.material.TypographyKt.access$withDefaultFontFamily(Typography.kt:1)
at androidx.compose.material.Typography.<init>(Typography.kt:186)
at androidx.compose.material.Typography.<init>(Typography.kt:118)
at com.example.widgettest.ui.theme.TypeKt.<clinit>(Type.kt:10)
at com.example.widgettest.ui.theme.ThemeKt.WidgetTestTheme(Theme.kt:40)
at com.example.widgettest.MainActivityKt.DefaultPreview(MainActivity.kt:35)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableMethod(PreviewUtils.kt:141)
at androidx.compose.ui.tooling.preview.PreviewUtilsKt.invokeComposableViaReflection(PreviewUtils.kt:180)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3$1$composable$1.invoke(ComposeViewAdapter.kt:545)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3$1$composable$1.invoke(ComposeViewAdapter.kt:543)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.kt:582)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3$1.invoke(ComposeViewAdapter.kt:538)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:64)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:487)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$WrapPreview$1.invoke(ComposeViewAdapter.kt:486)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:486)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt:112)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:538)
at androidx.compose.ui.tooling.preview.ComposeViewAdapter$init$3.invoke(ComposeViewAdapter.kt:535)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:384)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:228)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:227)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:148)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:114)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals$3.invoke(AndroidCompositionLocals.android.kt:113)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:106)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:162)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1$3.invoke(Wrapper.android.kt:161)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:161)
at androidx.compose.ui.platform.WrappedComposition$setContent$1$1.invoke(Wrapper.android.kt:144)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.ComposerKt.invokeComposable(Composer.kt:3324)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2575)
at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2571)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(SnapshotState.kt:523)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2564)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2515)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:476)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:727)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:432)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:144)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:135)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:724)
at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:135)
at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:187)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:142)
at androidx.compose.ui.platform.WrappedComposition$setContent$1.invoke(Wrapper.android.kt:135)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:811)
at android.view.View.dispatchAttachedToWindow(View.java:20479)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3496)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:44)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:360)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:714)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$7(RenderTask.java:870)
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:834)
Solution 1:[1]
Faced a similar problem. Solved by invalidating caches and restarting studio (from the File Menu), then rebuilding project.
Solution 2:[2]
Make sure to reference androidx.compose.ui:ui
in the build.gradle
.
Solution 3:[3]
I can run it with a manually filled-in configuration. I made a new run configuration and set the path. Maybe it is a bug.
Tool:Android Studio Bumblebee | 2021.1.1 Canary 7
Solution 4:[4]
- Be sure you have latest Android Studio version
- Within project/build.gradle be sure you have
dependencies { classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20' }
- Within app/build.gradle be sure you have
android { composeOptions { kotlinCompilerExtensionVersion'1.1.1' } buildFeatures { compose true } } dependencies { implementation("androidx.compose.ui:ui:1.1.1") // Tooling support (Previews, etc.) debugImplementation "androidx.compose.ui:ui-tooling:1.1.1" implementation "androidx.compose.ui:ui-tooling-preview:1.1.1" // Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.) implementation("androidx.compose.foundation:foundation:1.1.1") // Material Design implementation("androidx.compose.material:material:1.1.1") // Material design icons implementation("androidx.compose.material:material-icons-core:1.1.1") implementation("androidx.compose.material:material-icons-extended:1.1.1") // Integration with observables implementation("androidx.compose.runtime:runtime-livedata:1.1.1") implementation("androidx.compose.runtime:runtime-rxjava2:1.1.1") }
- File -> Invalidate Ccaches and restart
- Run project once
Very important: check for correct @Preview import - should be:
import androidx.compose.ui.tooling.preview.Preview
Example:
@Composable fun SimpleComposable() { Text("Hello World") } @Preview @Composable fun ComposablePreview() { SimpleComposable() }
@Preview function should be without params.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richard Onslow Roper |
Solution 2 | Martin Zeitler |
Solution 3 | Ryan M |
Solution 4 | Liviu |