Kotlin 1.5.0 上的 Jetpack Compose

Posted

技术标签:

【中文标题】Kotlin 1.5.0 上的 Jetpack Compose【英文标题】:Jetpack Compose on Kotlin 1.5.0 【发布时间】:2021-08-08 12:36:02 【问题描述】:

我上周更新到 Kotlin 1.5,在昨天看到 Google 打算让 Jetpack Compose 成为设计 UI 的首选选项之后,我想做一些测试。

问题是我的项目更新到 Kotlin 1.5 时,尝试构建项目时出现以下错误:

This version (1.0.0-beta07) of the Compose Compiler requires Kotlin version 1.4.32 but you appear to be using Kotlin version 1.5.0 which is not known to be compatible.  Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

Kotlin 1.5 是否与 Jetpack Compose 不兼容?在谷歌搜索问题后,我发现了 1.5.0-M2 版本,其中提到了 Jetpack Compose,但不是以“不兼容”的方式。

你对此有什么答案吗?我应该使用suppressKotlinVersionCompatibilityCheck 吗?这种情况下,我可以直接添加到模块的build.gradle文件中的android.kotlinOptions吗?

提前致谢。

编辑 1: I've found 我应该添加 suppressKotlinVersionCompatibilityCheck 作为构建器 arg:

android 
    ...
    kotlinOptions 
        ...
        freeCompilerArgs += [
            "-P",
"plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
        ...
    
    ...

但它仍然没有编译,我从编译器得到这个错误:

java.lang.NoClassDefFoundError: org/jetbrains/kotlin/ir/descriptors/WrappedSimpleFunctionDescriptor
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.buildFunction(LiveLiteralTransformer.kt:842)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.irLiveLiteralGetter(LiveLiteralTransformer.kt:933)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitConst(LiveLiteralTransformer.kt:426)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitConst(IrElementTransformerVoid.kt:138)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitConst(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl.accept(IrConstImpl.kt:33)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1$3.invoke(LiveLiteralTransformer.kt:624)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1$3.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$enter(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1.invoke(LiveLiteralTransformer.kt:623)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitCall(LiveLiteralTransformer.kt:612)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:199)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl.accept(IrCallImpl.kt:47)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1$3.invoke(LiveLiteralTransformer.kt:624)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1$3.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$enter(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1.invoke(LiveLiteralTransformer.kt:623)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitCall$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitCall(LiveLiteralTransformer.kt:612)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:199)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl.accept(IrCallImpl.kt:47)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitBranch$1.invoke(LiveLiteralTransformer.kt:738)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitBranch$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitBranch(LiveLiteralTransformer.kt:737)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBranch(IrElementTransformerVoid.kt:255)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBranch(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.impl.IrBranchImpl.transform(IrWhenImpl.kt:56)
    at org.jetbrains.kotlin.ir.expressions.IrWhen.transformChildren(IrWhen.kt:37)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitExpression(IrElementTransformerVoid.kt:131)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitWhen(IrElementTransformerVoid.kt:247)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$visitWhen$s1031542550(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitWhen$1.invoke(LiveLiteralTransformer.kt:705)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitWhen$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:117)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor$siblings$1.invoke(DurableKeyVisitor.kt:131)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:131)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.siblings(LiveLiteralTransformer.kt:192)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitWhen(LiveLiteralTransformer.kt:704)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitWhen(IrElementTransformerVoid.kt:248)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitWhen(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.IrWhen.accept(IrWhen.kt:29)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:26)
    at org.jetbrains.kotlin.ir.expressions.IrBlockBody.transformChildren(IrBody.kt:62)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBody(IrElementTransformerVoid.kt:108)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBlockBody(IrElementTransformerVoid.kt:117)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$visitBlockBody$s1031542550(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitBlockBody$1.invoke(LiveLiteralTransformer.kt:795)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitBlockBody$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:117)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.siblings(LiveLiteralTransformer.kt:193)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitBlockBody(LiveLiteralTransformer.kt:794)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBlockBody(IrElementTransformerVoid.kt:118)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitBlockBody(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.IrBlockBody.accept(IrBody.kt:54)
    at org.jetbrains.kotlin.ir.expressions.IrBody.transform(IrBody.kt:27)
    at org.jetbrains.kotlin.ir.declarations.IrFunction.transformChildren(IrFunction.kt:69)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitDeclaration(IrElementTransformerVoid.kt:57)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunction(IrElementTransformerVoid.kt:69)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitSimpleFunction(IrElementTransformerVoid.kt:72)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$visitSimpleFunction$s1031542550(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitSimpleFunction$1.invoke(LiveLiteralTransformer.kt:654)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitSimpleFunction$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.enter(LiveLiteralTransformer.kt:191)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitSimpleFunction(LiveLiteralTransformer.kt:654)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitSimpleFunction(IrElementTransformerVoid.kt:73)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitSimpleFunction(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.declarations.IrSimpleFunction.accept(IrSimpleFunction.kt:29)
    at org.jetbrains.kotlin.ir.IrElement$DefaultImpls.transform(IrElement.kt:32)
    at org.jetbrains.kotlin.ir.IrElementBase.transform(IrElementBase.kt:19)
    at org.jetbrains.kotlin.ir.util.TransformKt.transformInPlace(transform.kt:35)
    at org.jetbrains.kotlin.ir.declarations.IrClass.transformChildren(IrClass.kt:67)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitDeclaration(IrElementTransformerVoid.kt:57)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitClass(IrElementTransformerVoid.kt:66)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$visitClass$s1031542550(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitClass$1.invoke(LiveLiteralTransformer.kt:450)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitClass$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:117)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor$siblings$1.invoke(DurableKeyVisitor.kt:131)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.enter(DurableKeyVisitor.kt:96)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:131)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.siblings(LiveLiteralTransformer.kt:192)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitClass(LiveLiteralTransformer.kt:449)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitClass(IrElementTransformerVoid.kt:67)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitClass(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.declarations.IrClass.accept(IrClass.kt:56)
    at org.jetbrains.kotlin.ir.IrElement$DefaultImpls.transform(IrElement.kt:32)
    at org.jetbrains.kotlin.ir.IrElementBase.transform(IrElementBase.kt:19)
    at org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl.transformChildren(IrFileImpl.kt:71)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitPackageFragment(IrElementTransformerVoid.kt:41)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFile(IrElementTransformerVoid.kt:47)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.access$visitFile$s1031542550(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitFile$1.invoke(LiveLiteralTransformer.kt:496)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer$visitFile$1.invoke(LiveLiteralTransformer.kt:158)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.siblings(DurableKeyVisitor.kt:117)
    at androidx.compose.compiler.plugins.kotlin.lower.DurableKeyVisitor.root(DurableKeyVisitor.kt:152)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.visitFile(LiveLiteralTransformer.kt:463)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFile(IrElementTransformerVoid.kt:48)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFile(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl.accept(IrFileImpl.kt:63)
    at org.jetbrains.kotlin.ir.declarations.IrFile.transform(IrFile.kt:48)
    at org.jetbrains.kotlin.ir.declarations.impl.IrModuleFragmentImpl.transformChildren(IrModuleFragmentImpl.kt:45)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoidKt.transformChildrenVoid(IrElementTransformerVoid.kt:330)
    at androidx.compose.compiler.plugins.kotlin.lower.LiveLiteralTransformer.lower(LiveLiteralTransformer.kt:169)
    at androidx.compose.compiler.plugins.kotlin.ComposeIrGenerationExtension.generate(ComposeIrGenerationExtension.kt:80)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory$convertToIr$1.invoke(JvmIrCodegenFactory.kt:120)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory$convertToIr$1.invoke(JvmIrCodegenFactory.kt:116)
    at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:91)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:140)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$default(JvmIrCodegenFactory.kt:66)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:61)
    at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:592)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:212)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:155)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:88)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
    at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:386)
    at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:110)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:303)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl$rebuild(IncrementalCompilerRunner.kt:99)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:124)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:74)
    at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:607)
    at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:96)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1659)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
    at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassNotFoundException: org.jetbrains.kotlin.ir.descriptors.WrappedSimpleFunctionDescriptor
    at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 164 more

【问题讨论】:

以下是要进行的更改:android-review.googlesource.com/c/platform/frameworks/support/+/… 【参考方案1】:

更新androidx.compose.compiler:compiler:1.0.0-beta08 已发布(2021 年 6 月 2 日)。

从这个版本开始,Kotlin 的预期版本是1.5.10

dependencies 
    implementation "androidx.compose.compiler:compiler:1.0.0-beta08"


android 
    buildFeatures 
        compose true
    

    composeOptions 
        kotlinCompilerVersion "1.5.10"
        kotlinCompilerExtensionVersion "1.0.0-beta08"
    

    kotlinOptions 
        jvmTarget = "1.8"
    

查看更多:

Compose Compose Compiler

Compose 编译器 1.0.0-beta07 尚不支持 Kotlin 1.5.0

而且我发现支持Kotlin1.5.0的改动已经合并了,但是还没有发布。

https://android-review.googlesource.com/c/platform/frameworks/support/+/1651538

目前,我认为你应该使用1.4.3并等待下一个版本(1.0.0:beta081.0.0

【讨论】:

compose_version = '1.0.0-beta09' 请注意kotlinCompilerVersion can be safely removed。 Compose 现在使用 buildscript 中定义的 kotlin 编译器。 正如下面的答案中提到的,您可以在构建脚本中更新 Kotlin 版本:classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10" 只要使用kotlinCompilerExtensionVersion 1.0.0 kotlinCompilerVersionscomposeOptions 中的使用已被弃用,此后 compose 仅​​使用项目级构建文件中定义的 kotlin 版本。现在,即使这样似乎也已被弃用,因为从 compose 1.0.1 开始,buildScript 中没有指定 kotlin 版本。现在怎么更新?事实上,“去哪里”?我无法构建项目。【参考方案2】:

这是我对最新 compose 版本所做的,直到我今天写为止。

compose_version = '1.2.0-alpha01'

1) 如果不需要,请删除以下行:

2) 如果需要,将您的 Kotlin 版本更新为最终版本 -> click this link

3)终于在build.gradle (:project)更新compose版本:

ext 
    compose_version = '1.2.0-alpha01'

还有 gradle 插件:

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

现在你不应该对最新版本有任何警告。

【讨论】:

为我工作,谢谢!【参考方案3】:

在 build.gradle 文件(项目)中:

更改classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"

在build.gradle文件(app)中:

composeOptions 
            kotlinCompilerExtensionVersion compose_version
            kotlinCompilerVersion kotlin_version
        

compose_version 将是 1.0.1

kotlin_version 将是 1.5.21

【讨论】:

"ComposeOptions.kotlinCompilerVersion 已弃用。Compose 现在使用您的 buildscript 中定义的 kotlin 编译器。"【参考方案4】:

[update] compose_version = '1.0.0' 现在对于 Kotlin 1.5.10 是稳定的

buildscript 
    ext 
        compose_version = '1.0.0'
    
    repositories 
        google()
        mavenCentral()
    
    dependencies 
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
    

【讨论】:

compose_version = '1.0.0-beta09' 现在可用于 Kotlin 1.5 你说得对,我通过将所有 compose 依赖项移动到 1.0.0 修复了这个 kotlin 1.5.10 兼容性错误,谢谢!【参考方案5】:

Compose 编译器的此版本 (1.0.0-beta07) 需要 Kotlin 版本 1.4.32,但您似乎使用的是未知兼容的 Kotlin 版本 1.5.0。请修正你的配置(或suppressKotlinVersionCompatibilityCheck,但不要说我没有警告你!)

Compose 编译器的此版本 (1.0.0-beta08) 需要 Kotlin 版本 1.5.10,但您似乎使用的是不兼容的 Kotlin 版本 1.4.31。请修正你的配置(或suppressKotlinVersionCompatibilityCheck,但不要说我没有警告你!)。

版本 1.0.0-beta07 需要 Kotlin 1.4.32

版本 1.0.0-beta08 需要 Kotlin 1.5.10

【讨论】:

【参考方案6】:

我的问题通过将 Kotlin 从 1.5.20 降级到 1.5.10,并在 build.gradle 文件中添加 kotlinCompilerVersion "1.5.10"composeOptions 来解决。像这样:

composeOptions 
    kotlinCompilerVersion "1.5.10"
    kotlinCompilerExtensionVersion '1.0.0-rc01'

【讨论】:

【参考方案7】:

在这里查看同样的问题Where to specify Kotlin Version for Compose as of Compose 1.0.1?

截至 Compose 1.0.1 和 Studio BumbleBee 最新更新(截至目前),compose 所需的 Kotlin 版本是 1.5.10,但是 Bumblebee 更新即使在 @987654322 中也不包含 kotlin 版本声明@ 堵塞。现在,您必须更改 settings.gradle 文件中声明的依赖项,这也是工作室从中获取它的地方。现在,请注意,即使您在项目级构建文件的buildScript 块中声明了正确的版本,项目也不会编译,因为工作室仍会从同一位置获取版本。

另外,如果您很难确定相应版本的 Compose 需要哪个版本,您可以创建项目,当它失败时,可以选择 runscan 来查看完整日志输出,它公开了相同的内容。或者,您可以在设置中对其进行编辑,使其成为默认值,无论如何。

【讨论】:

【参考方案8】:

我在尝试将旧项目迁移到 compose 时遇到了这个问题。对我来说,解决方案是创建一个撰写活动。这可以手动完成,也可以从图库中创建一个空的撰写活动。

【讨论】:

【参考方案9】:

我收到这样的错误

e: Compose 编译器的这个版本 (1.0.0) 需要 Kotlin 版本 1.5.10 但您似乎使用的是 Kotlin 版本 1.5.20 不知道是否兼容。请修复您的配置(或 suppressKotlinVersionCompatibilityCheck 但不要说我没有警告 你!)。

然后我打开 build.gradle(project) 并更改

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20'          

到这里

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10' 

不要忘记同步并运行项目。希望这对你也有用!

【讨论】:

【参考方案10】:

添加新模块时遇到此问题。修复将以下内容添加到新模块:

buildFeatures 
  compose true


composeOptions 
  kotlinCompilerExtensionVersion compose_version

1.0.0-beta07 似乎是它在没有选择版本时选择的。

【讨论】:

【参考方案11】:

制作所有(本页上方)https://***.com/a/69508800/13432944

主要看这几行

composeOptions 
    kotlinCompilerExtensionVersion "1.0.4"

Android Studio Screenshot

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于Kotlin 1.5.0 上的 Jetpack Compose的主要内容,如果未能解决你的问题,请参考以下文章

Android Jetpack 是不是需要使用 Kotlin 语言?

使用Kotlin+JetPack 从零开发自己的日记App

Kotlin 和 Jetpack 视频合集 | MAD Skills

Kotlin基础从入门到进阶系列讲解(进阶篇)Jetpack,(更新中)

Kotlin基础从入门到进阶系列讲解(进阶篇)Jetpack,(更新中)

kotlin协程的生命周期与jetpack组件绑定