gpt4 book ai didi

android - 升级到 Jetpack Compose Alpha 12 会导致 setContent 出现错误

转载 作者:行者123 更新时间:2023-12-03 13:24:56 25 4
gpt4 key购买 nike

我升级到 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


因此,要修复您的应用程序,您应该:
  • 删除 freeCompilerArgs += "-Xallow-unstable-dependencies"来自 build.gradle 的行文件(因为不再需要)
  • 添加对 Activity Compose 的特定依赖 1.3.0-alpha02 :
  • implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
    通过添加该依赖关系, setContent 的任何直接使用以及 androidx.compose.ui:ui-tooling:1.0.0-alpha12 的内部使用或 androidx.compose.ui:ui-test-junit4:1.0.0-alpha12将使用固定的 Activity Compose 1.3.0-alpha02 版本。

    关于android - 升级到 Jetpack Compose Alpha 12 会导致 setContent 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66146594/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com