【问题标题】:Upgrading to Jetpack Compose Alpha 12 causes errors on setContent升级到 Jetpack Compose Alpha 12 会导致 setContent 出现错误
【发布时间】:2021-05-14 17:07:17
【问题描述】:

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

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

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

ComponentActivity.setContent 已移至 androidx.activity:activity-compose 模块中的 androidx.activity.compose.setContent。 (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?

【问题讨论】:

    标签: android android-activity android-jetpack-compose


    【解决方案1】:

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

    从那个问题:

    Activity 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. 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 版本。

    【讨论】:

    • 嗨,有没有资源可以以一种易于理解的方式将所有这些变化联系在一起?
    【解决方案2】:

    使用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

    【讨论】:

    • 如果不是关于setContent,那么它真的不是这个问题的答案,是吗?听起来您遇到了与迁移到 Kotlin 1.4.3.0 相关的完全不同的问题,即 this Kotlin issue(与 Compose 无关)和使用您留下的不再需要的标志。
    • 你是对的。对此提出了另一个新问题。 stackoverflow.com/questions/66343654/…
    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 2020-11-16
    • 2020-04-07
    • 2020-02-10
    • 2021-12-17
    • 1970-01-01
    • 2022-01-08
    • 2021-02-16
    相关资源
    最近更新 更多