如何修复 Jetpack Compose 的预览?

问题描述 投票:0回答:3

我尝试在现有项目中使用 Jetpack Compose,问题是如果我包含

@Preview
注释,它不会向我显示任何内容,只是这个

这是我的代码

@Preview
@Composable
fun test() {
    Text(text = "HELLO")
}

我在这里缺少什么?

UPD

我认为这个问题与我将 Compose 添加到现有项目这一事实有关,就好像我尝试从头开始创建项目一样,它运行得很好。

另外我想添加错误截图

android android-jetpack-compose android-jetpack jetpack
3个回答
2
投票

添加这些:

debugImplementation "androidx.compose.ui:ui-tooling:1.2.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.2.1"

也添加这些:

debugImplementation "androidx.customview:customview:1.2.0-alpha01"
debugImplementation "androidx.customview:customview-poolingcontainer:1.0.0"

为我工作 Android Studio 花栗鼠 | 2021.2.1 补丁 2,
AGP 7.2.2,
Compose 编译器 1.3.1,
撰写 1.2.1

[来源:https://issuetracker.google.com/issues/227767363#comment2]


1
投票

确保您的项目中有以下依赖项

debugImplementation "androidx.compose.ui:ui-tooling:1.2.1"
implementation "androidx.compose.ui:ui-tooling-preview:1.2.1"

此外,您使用的 Android Gradle 插件版本与您正在使用的 Android Studio 版本相匹配。


0
投票

我能够通过替换使其工作

debugImplementation("androidx.compose.ui:ui-tooling")

implementation("androidx.compose.ui:ui-tooling")

这不是最佳选择,因为该库将包含在发布版本中,但它可以作为临时修复。

© www.soinside.com 2019 - 2024. All rights reserved.