升级到Jetpack Compose Alpha 12会导致setContent错误 [英] Upgrading to Jetpack Compose Alpha 12 causes errors on setContent
问题描述
我升级到Jetpack Compose 1.0.0-alpha12并开始遇到问题.
首先,我使用的 setContent
方法显示为已弃用.
摘录自 Alpha 12发行说明,我注意到它说:
ComponentActivity.setContent已移至androidx.activity:activity-compose模块中的androidx.activity.compose.setContent.( Icf416 )
因此,我删除了 import androidx.compose.ui.platform.setContent
,并将其切换为 import androidx.activity.compose.setContent
,从而删除了弃用项./p>
但是,我然后看到一条错误消息:
w:此版本的编译器不支持标志:-Xallow-jvm-ir-dependenciesw:注意!此版本使用不安全的内部编译器参数:-XXLanguage:+ NonParenthesizedOnFunctionalTypes上的注释不建议将此模式用于生产环境,因为没有提供稳定性/兼容性保证编译器或生成的代码.需要您自担风险使用它!e:在依赖项中发现了由不稳定版本的Kotlin编译器编译的类.从类路径中删除它们,或使用"-Xallow-unstable-dependencies"抑制错误e:/[我的路径]/MainActivity.kt:(39,9):类'androidx.activity.compose.ComponentActivityKt'是由不稳定版本的Kotlin编译器编译,因此无法由该编译器加载
再次,我可以通过将 build.gradle
文件更改为具有以下方法来解决此问题:
kotlinOptions {jvmTarget ='1.8'useIR = true//我添加了这一行freeCompilerArgs + ="-Xallow不稳定依赖性";}
虽然可以编译我的应用程序,但现在在运行时出现以下异常:
java.lang.NoSuchMethodError:没有静态方法setContent(Landroidx/activity/ComponentActivity; Landroidx/compose/runtime/CompositionContext; Lkotlin/jvm/functions/Function2;)V in classLandroidx/活动/撰写/ComponentActivityKt;或它的超类("androidx.activity.compose.ComponentActivityKt"的声明出现在[我的apk]中
如何解决此问题并将我的应用程序升级到Jetpack Compose 1.0.0-alpha12?
根据此问题,此问题与新的 androidx.activity:activity-compose:1.3.0-alpha01
工件有关.
从该问题开始:
活动
1.3.0-alpha02
已发布并解决了此问题.使用Compose alpha12的应用,尤其是内部使用
setContent
的工件(例如androidx.compose.ui:ui-test-junit4:1.0.0-alpha12
)应添加activity-compose:1.3.0-alpha02
依赖于其dependencies
块,以确保不使用1.3.0-alpha01
工件
因此,要修复您的应用,您应该:
-
从
build.gradle
文件中删除freeCompilerArgs + ="-Xallow-unstable-dependencies"
行(因为不再需要) -
添加对Activity Compose
1.3.0-alpha02
的特定依赖项:
实现'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版本.
I upgraded to Jetpack Compose 1.0.0-alpha12 and started to run into issues.
Firstly, the setContent
method I was using showed as deprecated.
From the Alpha 12 release notes, I noticed that it said:
ComponentActivity.setContent has moved to androidx.activity.compose.setContent in the androidx.activity:activity-compose module. (Icf416)
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
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]
How can I fix this and upgrade my app to Jetpack Compose 1.0.0-alpha12?
As per this issue, this issue is related to the new androidx.activity:activity-compose:1.3.0-alpha01
artifact.
From that issue:
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 usesetContent
should add theactivity-compose:1.3.0-alpha02
dependency to theirdependencies
block to ensure that the1.3.0-alpha01
artifact is not used
So to fix your app, you should:
Remove the
freeCompilerArgs += "-Xallow-unstable-dependencies"
line from thebuild.gradle
file (as it is no longer needed)Add a specific dependency on Activity Compose
1.3.0-alpha02
:
implementation 'androidx.activity: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屋!