Jetpack Compose 预览在多模块应用程序中失败

Jetpack Compose Preview fails in Multi Module app

我有一个多模块应用程序,我最近开始在其中实现 Jetpack Compose。我定义了一些由不同模块共享的可组合项。我将它们放在另一个库模块中并导入到相关位置。可组合示例:

// Nothing special here, any composable function fails the same way
@Composable
fun AppTheme(content: @Composable () -> Unit) {
    val appColors = lightColors(
        primary = Blue
    )
    MaterialTheme(colors = appColors, content = content)
}

一切似乎都工作正常,除了所说的可组合项无法构建预览:

@Preview(showSystemUi = true, showBackground = true)
@Composable
fun HistoryScreenPreview() {
    AppTheme {
        HistoryScreen()
    }
}

在上面的示例中,如果我删除“AppTheme”,预览效果非常好。

堆栈跟踪:

java.lang.NoSuchMethodError: 'void com.somepackage.lib.ui_theme.AppComposablesKt.AppTheme(_layoutlib_._internal_.kotlin.jvm.functions.Function2, androidx.compose.runtime.Composer, int)'
at com.somepackage.feature.home.ui.history.view.HistoryFragment.HistoryScreenPreview(HistoryFragment.kt:238)
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.CommonPreviewUtils.invokeComposableMethod(CommonPreviewUtils.kt:150)
at androidx.compose.ui.tooling.CommonPreviewUtils.invokeComposableViaReflection$ui_tooling_release(CommonPreviewUtils.kt:194)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt:593)
at androidx.compose.ui.tooling.ComposeViewAdapter$init$composable.invoke(ComposeViewAdapter.kt:591)
at androidx.compose.ui.tooling.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:630)
at androidx.compose.ui.tooling.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:586)
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:228)
at androidx.compose.ui.tooling.InspectableKt.Inspectable(Inspectable.kt:61)
at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt:535)
at androidx.compose.ui.tooling.ComposeViewAdapter$WrapPreview.invoke(ComposeViewAdapter.kt:534)
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:228)
at androidx.compose.ui.tooling.ComposeViewAdapter.WrapPreview(ComposeViewAdapter.kt:530)
at androidx.compose.ui.tooling.ComposeViewAdapter.access$WrapPreview(ComposeViewAdapter.kt:121)
at androidx.compose.ui.tooling.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:586)
at androidx.compose.ui.tooling.ComposeViewAdapter$init.invoke(ComposeViewAdapter.kt:583)
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:410)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.android.kt:252)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated.invoke(ComposeView.android.kt:251)
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:228)
at androidx.compose.ui.platform.CompositionLocalsKt.ProvideCommonCompositionLocals(CompositionLocals.kt:166)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals.invoke(AndroidCompositionLocals.android.kt:123)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$ProvideAndroidCompositionLocals.invoke(AndroidCompositionLocals.android.kt:122)
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:228)
at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.ProvideAndroidCompositionLocals(AndroidCompositionLocals.android.kt:114)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:157)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:156)
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:228)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:156)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:140)
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:3337)
at androidx.compose.runtime.ComposerImpl$doCompose.invoke(Composer.kt:2582)
at androidx.compose.runtime.ComposerImpl$doCompose.invoke(Composer.kt:2571)
at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:247)
at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source)
at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2571)
at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2522)
at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:478)
at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:748)
at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:433)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:140)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(AndroidComposeView.android.kt:907)
at androidx.compose.ui.platform.WrappedComposition.setContent(Wrapper.android.kt:131)
at androidx.compose.ui.platform.WrappedComposition.onStateChanged(Wrapper.android.kt:182)
at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:196)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:138)
at androidx.compose.ui.platform.WrappedComposition$setContent.invoke(Wrapper.android.kt:131)
at androidx.compose.ui.platform.AndroidComposeView.onAttachedToWindow(AndroidComposeView.android.kt:994)
at android.view.View.dispatchAttachedToWindow(View.java:20753)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3490)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3497)
at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:57)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:368)
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:736)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:892)
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 工作室版本:

编写版本: 1.1.1

我发现了它在运行时也会失败的问题。结果我在定义 AppTheme 等模块的 build.gradle 文件中没有“compose = true”。换句话说,每个 build.gradle 文件都需要单独指定这些设置。