【问题标题】:Jetpack compose can't preview after updating to 1.0.0-rc01Jetpack compose 更新到 1.0.0-rc01 后无法预览
【发布时间】:2021-09-14 08:37:29
【问题描述】:

这仅在我更新到 1.0.0-rc01 时发生在我身上。 它说:

找不到以下类: - androidx.compose.ui.tooling.preview.ComposeViewAdapter(修复构建路径、编辑 XML、创建类)

我的代码:

@Composable
@Preview
fun CenterProgress(){
    Box(
        modifier= Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ){
        CircularProgressIndicator(strokeWidth = 3.dp)
    }
}

【问题讨论】:

  • 根据 Kotlinlang Slack 的 #compose 频道上的评论,在升级到北极狐 RC1 之前,预览将无法使用,遗憾的是目前尚不可用。 ???同样的评论建议您可以将工具依赖项放回beta09,它们将与rc01 一起使用主要依赖项。请注意,我还没有尝试过,所以我不知道它是否有效。
  • 不幸的是退回到 beta09 对我仍然不起作用
  • 回退到 beta09 对我也不起作用。用北极狐 5、北极狐 2 和大黄蜂尝试过
  • @CommonsWare 提到的解决方案在 Bumblebee Canary 3 上对我有用。
  • 如果你想使用稳定的compose版本试试这个jetpackcompose.net/post/jetpack-compose-preview-not-showing

标签: android android-jetpack-compose


【解决方案1】:

2021 年 7 月 20 日更新: 只需下载并使用latest AS 即可解决问题


他们在 rc01 中拆分了一些包,但根据 @CommonsWare 评论(所有功劳归于他)看来,Android Studio 版本本身存在问题。 你有两个选择:

  1. 降级到 beta09,直到 AS ArcticFox RC1 结束
  2. 尝试建议的解决方法,使用 AS Arctic Fox Beta 5,将所有撰写依赖项保留为 1.0.0-rc01 版本,仅将 ui-tooling 降级为 1.0.0-beta09(由 cmets 确认)。

更多细节

在这里你可以找到他们在1.0.0-rc01https://android-review.googlesource.com/c/platform/frameworks/support/+/1739498 中移动的所有课程以及为什么会这样决定的解释。

简而言之,您现在可以针对一些特定的优化场景(不应该是默认情况)执行此操作:

debugImplementation "androidx.compose.ui:ui-tooling:1.0.0-rc01"
implementation "androidx.compose.ui:ui-tooling-preview:1.0.0-rc01"

【讨论】:

  • 我试过了,我得到了这个无法访问'ComposeViewAdapter':它在'androidx.compose.ui.tooling'中是内部的
  • 这很奇怪......你在 build.gradle 中更新了kotlinCompilerExtensionVersion 吗?可能是 AS 本身的问题
  • Android Studio Artic Fox (2020.3.1) Beta 5 - 2021 年 7 月 2 日。对我来说,它解决了问题,将所有 compose 依赖项保留为 1.0.0-rc01 版本并仅降级 ui-tooling到 1.0.0-beta09。
  • 今天下载了最新的可用 AS,创建了一个新项目并将内容更新到 rc01,为了能够预览,我必须使用 debugImplementation 的 ui-tooling:1.0.0-beta07"; 而不是 rc01。跨度>
  • 因为 RC1 出来了,我的问题似乎已经解决了
【解决方案2】:

更新:Android Studio Bumblebee 不再需要此功能 | 2021.1.1 Canary 6 和 Android Gradle 插件 7.1.0-alpha06。注意:Canary 4 已经修复了这个问题,但需要一个损坏的 AGP 版本。现在也解决了。

除了上面的答案:这里是如何在gradle中强制使用ui-tooling版本:

implementation("androidx.compose.ui:ui-tooling:$compose_version") {
    version {
        // TODO: Remove this when Android Studio has become compatible again
        // Android Studio Bumblebee | 2021.1.1 Canary 3 is not compatible with module ui-tooling 1.0.0-rc01 or higher.
        // The Run Configuration for Composable Previews that Android Studio makes expects a PreviewActivity class
        // in the `androidx.compose.ui.tooling.preview` package, but it was moved in 1.0.0-rc01, and thus causes error:
        // "androidx.compose.ui.tooling.preview.PreviewActivity is not an Activity subclass or alias".
        // For more, see: https://stackoverflow.com/questions/68224361/jetpack-compose-cant-preview-after-updating-to-1-0-0-rc01
        strictly("1.0.0-beta09")
    }
}

【讨论】:

  • 这个解决方案很好用,非常感谢
【解决方案3】:

就我而言,这是因为我在“发布”时留下了构建变体。 将其改回“调试”可修复缺少的类错误。

预览功能可能来自 build.gradle 中的以下行

debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

【讨论】:

  • 好地方 - 对我来说也是正确的问题。
【解决方案4】:

我像上面的评论一样尝试过,它实际上对我有帮助,只是我必须删除 :

Artic Fox Beta 5 的链接:
https://developer.android.com/studio/preview

androidx.compose.ui:ui-tooling-preview.
我的撰写配置如下所示:

android {

def compose_version = '1.0.0-rc01'


composeOptions {
  kotlinCompilerExtensionVersion "$compose_version"
}

dependencies {
  def compose_version = '1.0.0-rc01'
/**Compose Related*/

    implementation "androidx.compose.compiler:compiler:$compose_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.activity:activity-compose:1.3.0-rc01"
    implementation "androidx.compose.material:material:$compose_version"

    implementation "androidx.compose.ui:ui-tooling:1.0.0-beta09"
//    Need to comment this two lines to work on artic fox
//    implementation "androidx.compose.ui:ui-tooling:$compose_version"
//    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    /**Accompanist*/
    // Coil
    implementation 'dev.chrisbanes.accompanist:accompanist-insets:0.6.2'
    implementation "com.google.accompanist:accompanist-coil:0.13.0"
    implementation "androidx.compose.runtime:runtime:$compose_version"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

    /** Material Icons */
    implementation "androidx.compose.material:material-icons-extended:$compose_version"


    // Jetpack Compose Integration
    implementation "androidx.navigation:navigation-compose:2.4.0-alpha04"
}

}

【讨论】:

    【解决方案5】:

    降级 ui-tooling 库时仍然会出现错误:

    确保您没有依赖于 ui-tooling:1.0.0-rc01 的库 您可以通过在您的 android studio 终端中使用 ./gradlew app:dependencies 来发现这一点

    就我而言,我使用的是 com.google.accompanist:accompanist-swiperefresh:13.0.0,它依赖于 ui-tooling:1.0.0-rc01。当我降级到 accompanist-swiperefresh:12.0.0

    时,预览工作正常

    【讨论】:

    • 提到的伴奏版本不正确,应该是0.13.0和0.12.0。除此之外,很棒的发现!解决了我的问题。
    猜你喜欢
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多