升级到JetpackComposeAlpha12会导致setContent错误

我升级到 Jetpack Compose 1.0.0-alpha12 并开始遇到问题。

首先,setContent我使用的方法显示为已弃用。

从Alpha 12 发行说明中,我注意到它说:

ComponentActivity.setContent 已移至 androidx.activity:activity-compose 模块中的 androidx.activity.compose.setContent。( icf416 )

所以我删除了 myimport androidx.compose.ui.platform.setContent并将其切换为import androidx.activity.compose.setContent,从而删除了弃用。

但是,然后我收到一个错误消息:

w: Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies
w: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+NonParenthesizedAnnotationsOnFunctionalTypes
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
e: Classes compiled by an unstable version of the Kotlin compiler were found in dependencies.
Remove them from the classpath or use '-Xallow-unstable-dependencies' to suppress errors
e: /[my path]/MainActivity.kt: (39, 9): Class 'androidx.activity.compose.ComponentActivityKt' is
compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler

再一次,我能够通过将我的build.gradle文件更改为以下内容来解决这个问题:

kotlinOptions {
    jvmTarget = '1.8'
    useIR = true
    // I added this line
    freeCompilerArgs += "-Xallow-unstable-dependencies"
}

虽然这让我可以编译我的应用程序,但我现在在运行时遇到以下异常:

java.lang.NoSuchMethodError: No static method setContent(
Landroidx/activity/ComponentActivity;Landroidx/compose/runtime/Com
positionContext;Lkotlin/jvm/functions/Function2;)V in class 
Landroidx/activity/compose/ComponentActivityKt; or its super classes
(declaration of 'androidx.activity.compose.ComponentActivityKt' appears in [my apk]

如何解决此问题并将我的应用程序升级到 Jetpack Compose 1.0.0-alpha12?

回答

根据此问题,此问题与新androidx.activity:activity-compose:1.3.0-alpha01工件有关。

从那个问题:

活动1.3.0-alpha02已发布并修复了此问题。

使用 Compose alpha12 的应用程序,特别是像androidx.compose.ui:ui-test-junit4:1.0.0-alpha12内部使用的工件,setContent应该将activity-compose:1.3.0-alpha02依赖项添加到它们的dependencies块中,以确保1.3.0-alpha01不使用工件

因此,要修复您的应用程序,您应该:

  1. freeCompilerArgs += "-Xallow-unstable-dependencies"build.gradle文件中删除该行(因为不再需要它)

  2. 添加对 Activity Compose 的特定依赖项1.3.0-alpha02

implementation 'androidx.activity:activity-compose:1.3.0-alpha02'

通过添加该依赖项,或将使用固定的 Activity Compose 1.3.0-alpha02 版本的任何直接使用setContent以及内部使用。androidx.compose.ui:ui-tooling:1.0.0-alpha12androidx.compose.ui:ui-test-junit4:1.0.0-alpha12


以上是升级到JetpackComposeAlpha12会导致setContent错误的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>