升级到 Jetpack Compose Alpha 12 导致 setContent 出错

Upgrading to Jetpack Compose Alpha 12 causes errors on setContent

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

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

Alpha 12 release notes,我注意到它说:

ComponentActivity.setContent has moved to androidx.activity.compose.setContent in the androidx.activity:activity-compose module. (Icf416)

所以我删除了我的 import 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?

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

来自那一期:

Activity 1.3.0-alpha02 has been released and fixes this issue.

Apps using Compose alpha12 and specifically artifacts like androidx.compose.ui:ui-test-junit4:1.0.0-alpha12 that internally use setContent should add the activity-compose:1.3.0-alpha02 dependency to their dependencies block to ensure that the 1.3.0-alpha01 artifact is not used

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

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

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

implementation 'androidx.activity: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 的内部使用都将使用固定的 Activity Compose 1.3.0- alpha02 发布。

使用 Activity 1.3.0-alpha02setContent 正常,但出现另一个错误。

Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeJavaResWorkAction
   > 2 files found with path 'META-INF/AL2.0' from inputs:

必须使用解决方法来构建它

    packagingOptions {
        exclude 'META-INF/AL2.0'
        exclude 'META-INF/LGPL2.1'
    }

并且仍然有警告 Flag is not supported by this version of the compiler: -Xallow-jvm-ir-dependencies