【问题标题】:Jetpack Compose 1.0.4 Preview & Runtime ErrorJetpack Compose 1.0.4 预览和运行时错误
【发布时间】:2021-11-04 23:24:14
【问题描述】:

我正在尝试将现有的 android 应用程序转换为 compose。鉴于以下依赖项,我在应用启动时和预览窗口中收到以下错误:

The following classes could not be instantiated:
- androidx.compose.ui.tooling.ComposeViewAdapter

java.lang.NoSuchMethodError: 'androidx.lifecycle.LifecycleRegistry androidx.lifecycle.LifecycleRegistry.createUnsafe(androidx.lifecycle.LifecycleOwner)'
    at androidx.compose.ui.tooling.ComposeViewAdapter$FakeSavedStateRegistryOwner$1.<init>(ComposeViewAdapter.kt:705)
    at androidx.compose.ui.tooling.ComposeViewAdapter.<init>(ComposeViewAdapter.kt:704)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:399)
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:189)
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:147)
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:303)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:417)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:428)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:332)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:657)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:353)
    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)

这些是我的依赖项:

    implementation "com.google.accompanist:accompanist-appcompat-theme:0.16.0"

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation "androidx.compose.ui:ui:1.1.0-beta01"
    implementation "androidx.compose.material:material:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui-tooling-preview:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui-tooling:$rootProject.composeVersion"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    implementation "androidx.compose.runtime:runtime-livedata:$rootProject.composeVersion"

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$rootProject.composeVersion"

rootProject.composeVersion = "1.0.4"

我就现有问题听取了很多建议,甚至确定这不是我的 Android Studio 的问题,因为我已经能够使用 compose 创建干净的项目了。

更新(基于发布可组合代码的请求)

即使是这个可组合的代码也无法编译让我预览

@Composable
fun TestComposable() =
    Text(text = "This is working.")

@Preview(showSystemUi = true, showBackground = true)
@Composable
fun TestComposablePreview() =
    TestComposable()

【问题讨论】:

  • 这个androidx.compose.ui:ui:1.1.0-beta01也应该依赖于compose版本,比如androidx.compose.ui:ui:$rootProject.composeVersion
  • @JüriKin 尝试改用Preview(showSystemUi=true, showBackground=true)
  • 也许您应该包含您的可组合代码,以便其他人更好地理解。
  • 它试图预览什么可组合代码似乎并不重要。我用上面的更新创建了一个新文件,它仍然没有编译成功。 @PhilipDukhov 我将 ui:ui 更新为 composeVersion,但这似乎不起作用。 .MARSK 我的预览中确实有这个。我在上面更新了一些仍然无法编译预览的测试可组合代码。
  • 我对所有 compose 依赖项使用 1.0.1 版本。这很好用。你能试试吗?而activity-compose是1.3.0-alpha06。这两个只是我看到的区别。

标签: android kotlin android-jetpack-compose


【解决方案1】:

我终于找到了解决办法。问题在于生命周期版本控制。有代码意外地强制所有生命周期依赖项为相同的值,其中一些模块仍在使用生命周期扩展,因此 Gradle 试图强制生命周期扩展使用 2.4.0,这显然不存在已弃用。

删除它,并使用 2.4.0 使用所有独立的生命周期依赖项,它可以正常工作。

【讨论】:

  • 我知道我们有同样的问题,但你应该提供更多关于你如何解决问题的背景信息。
  • 我绝对认为这里有足够的上下文。我的问题不在于任何开箱即用的 Android 代码,而是我项目中现有的遗留代码。这里的上下文是我删除了旧代码,并确保我没有强制所有依赖项使用与依赖组名称相同的版本。然后,我可以根据需要专门对任何生命周期依赖项进行版本控制。
猜你喜欢
  • 1970-01-01
  • 2021-10-13
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2022-11-12
相关资源
最近更新 更多