[英]Jetpack compose can't preview after updating to 1.0.0-rc01
this happened to me only when I updated to 1.0.0-rc01.只有当我更新到 1.0.0-rc01 时才会发生这种情况。 it says:
它说:
The following classes could not be found: - androidx.compose.ui.tooling.preview.ComposeViewAdapter (Fix Build Path, Edit XML, Create Class)
找不到以下类:- androidx.compose.ui.tooling.preview.ComposeViewAdapter(修复构建路径、编辑 XML、创建类)
my code:我的代码:
@Composable
@Preview
fun CenterProgress(){
Box(
modifier= Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
){
CircularProgressIndicator(strokeWidth = 3.dp)
}
}
Update 07/20/2021: Just download and use the latest AS ArcticFox RC1 to fix the problem 2021 年 7月 20 日更新:只需下载并使用最新的AS ArcticFox RC1即可解决问题
They splitted some packages in rc01 but per @CommonsWare comment (all credits to him) it seems there is a problem with Android Studio version itself.他们在 rc01 中拆分了一些包,但根据 @CommonsWare 评论(所有功劳都归功于他),似乎 Android Studio 版本本身存在问题。 You have 2 options:
您有 2 个选择:
1.0.0-rc01
version and downgrade only ui-tooling
to 1.0.0-beta09
(confirmed by comments).1.0.0-rc01
版本,并仅将ui-tooling
降级到1.0.0-beta09
(由评论确认)。 Extra details额外的细节
Here you can find all the classes they moved in 1.0.0-rc01
https://android-review.googlesource.com/c/platform/frameworks/support/+/1739498 and the explanation on why this has been decided.在这里,您可以找到他们在
1.0.0-rc01
https://android-review.googlesource.com/c/platform/frameworks/support/+/1739498 中移动的所有类,以及为什么决定这样做的解释。
In short, you can now do this for some particular optimized scenarios (which should not be the default case):简而言之,您现在可以针对某些特定的优化场景(这不应该是默认情况)执行此操作:
debugImplementation "androidx.compose.ui:ui-tooling:1.0.0-rc01"
implementation "androidx.compose.ui:ui-tooling-preview:1.0.0-rc01"
For my case, it's because I left my build variant at "release".就我而言,这是因为我在“发布”时留下了我的构建变体。 Changing it back to "debug" fixes the missing class bug.
将其改回“调试”可修复丢失的类错误。
the preview functionality is probably from the following line in build.gradle预览功能可能来自 build.gradle 中的以下行
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
Update : This is no longer needed with Android Studio Bumblebee |更新: Android Studio Bumblebee不再需要此功能| 2021.1.1 Canary 4 .
2021.1.1 金丝雀 4 。 Warning : Note that this version of Android Studio requires to upgrade to Android Gradle Plugin 7.1.0-alpha04, which breaks running Compose apps from the IDE .
警告:请注意,此版本的 Android Studio 需要升级到 Android Gradle 插件 7.1.0-alpha04,这会中断从 IDE 运行 Compose 应用程序。 So, you may want to hold off a bit longer.
所以,你可能想推迟一段时间。
In addition to the above answers: here is how to force the ui-tooling version in gradle:除了上述答案:这里是如何在 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")
}
}
To people still get the error when downgrade the ui-tooling library:人们在降级 ui-tooling 库时仍然会收到错误消息:
Make sure you don't have library that have dependency on ui-tooling:1.0.0-rc01 You can find that out by using ./gradlew app:dependencies in your android studio terminal确保您没有依赖于 ui-tooling:1.0.0-rc01 的库您可以通过在 android studio 终端中使用./gradlew app:dependencies找到它
In my case, I'm using com.google.accompanist:accompanist-swiperefresh:13.0.0 which depend on ui-tooling:1.0.0-rc01.就我而言,我使用的是 com.google.accompanist:accompanist-swiperrefresh:13.0.0,它依赖于 ui-tooling:1.0.0-rc01。 Preview is working when I downgrade to accompanist-swiperefresh:12.0.0
当我降级到伴奏-swiperrefresh:12.0.0 时,预览正在工作
I tried it like the comment above me and it actually helped me only that i had to delete the implementation of :我像上面的评论一样尝试了它,它实际上对我有帮助,只是我不得不删除:
Link to Artic Fox Beta 5:链接到北极狐 Beta 5:
https://developer.android.com/studio/preview https://developer.android.com/studio/preview
androidx.compose.ui:ui-tooling-preview. androidx.compose.ui:ui-tooling-preview。
My compose config looks like the following:我的撰写配置如下所示:
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"
}
}
Adding the following dependency fixed my issue with the preview in a library module:添加以下依赖项解决了我在库模块中预览的问题:
debugImplementation 'androidx.customview:customview-poolingcontainer:1.0.0'
On my case, where I have a "design system" module providing all compose-related dependencies, I had to add the following dependency:就我而言,我有一个提供所有与 compose 相关的依赖项的“设计系统”模块,我必须添加以下依赖项:
implementation("androidx.activity:activity-compose:1.4.0")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.