升级到 Jetpack Compose Alpha 12 会导致 setContent 错误 [英] Upgrading to Jetpack Compose Alpha 12 causes errors on setContent
问题描述
我升级到 Jetpack Compose 1.0.0-alpha12 并开始遇到问题.
I upgraded to Jetpack Compose 1.0.0-alpha12 and started to run into issues.
首先,我使用的 setContent
方法显示为已弃用.
Firstly, the setContent
method I was using showed as deprecated.
来自 Alpha 12 发行说明,我注意到它说:
From the Alpha 12 release notes, I noticed that it said:
ComponentActivity.setContent 已移至 androidx.activity:activity-compose 模块中的 androidx.activity.compose.setContent.(Icf416)
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
,这删除了弃用.
So I removed my import androidx.compose.ui.platform.setContent
and switched it to import androidx.activity.compose.setContent
, which removed the deprecation.
但是,我收到了一个错误消息:
However, I then got an error that says:
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
文件来解决这个问题:
And again, I was able to work around that by changing my build.gradle
file to have:
kotlinOptions {
jvmTarget = '1.8'
useIR = true
// I added this line
freeCompilerArgs += "-Xallow-unstable-dependencies"
}
虽然让我编译我的应用程序,但我现在在运行时遇到以下异常:
While that let me compile my app, I now get the following exception at runtime:
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?
How can I fix this and upgrade my app to Jetpack Compose 1.0.0-alpha12?
推荐答案
根据这个问题,这个问题与新的 androidx.activity:activity-compose:1.3.0-alpha01
工件有关.
As per this issue, this issue is related to the new androidx.activity:activity-compose:1.3.0-alpha01
artifact.
来自那个问题:
活动 1.3.0-alpha02
已发布并修复了此问题.
Activity
1.3.0-alpha02
has been released and fixes this issue.
使用 Compose alpha12 的应用程序,特别是像 androidx.compose.ui:ui-test-junit4:1.0.0-alpha12
这样的内部使用 setContent
的工件应该添加 activity-compose:1.3.0-alpha02
对其 dependencies
块的依赖,以确保不使用 1.3.0-alpha01
工件
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
因此,要修复您的应用,您应该:
So to fix your app, you should:
从
build.gradle
文件中删除freeCompilerArgs += "-Xallow-unstable-dependencies"
行(因为不再需要它)
Remove the
freeCompilerArgs += "-Xallow-unstable-dependencies"
line from thebuild.gradle
file (as it is no longer needed)
添加对 Activity Compose 1.3.0-alpha02
的特定依赖:
Add a specific dependency on 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
或 的内部使用code>androidx.compose.ui:ui-test-junit4:1.0.0-alpha12
将使用固定的 Activity Compose 1.3.0-alpha02 版本.
By adding that dependency, any direct usages of setContent
as well as internal usages by androidx.compose.ui:ui-tooling:1.0.0-alpha12
or androidx.compose.ui:ui-test-junit4:1.0.0-alpha12
will use the fixed Activity Compose 1.3.0-alpha02 release.
这篇关于升级到 Jetpack Compose Alpha 12 会导致 setContent 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!