使用可点击修饰符时 Jetpack compose 编译错误

Posted

技术标签:

【中文标题】使用可点击修饰符时 Jetpack compose 编译错误【英文标题】:Jetpack compose compilation error when using clickable modifier 【发布时间】:2021-05-04 03:22:05 【问题描述】:

android Studio 更新为 Android Studio 北极狐后 | 2020.3.1 金丝雀 5 并将 gradle 更新为 7.0.0-alpha05 当我构建项目时,出现以下错误

java.lang.AssertionError: org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl@1b0927ba: No such type argument slot: 1
    at org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression.putTypeArgument(IrMemberAccessExpression.kt:49)
    at androidx.compose.compiler.plugins.kotlin.lower.ComposerLambdaMemoization.rememberExpression(ComposerLambdaMemoization.kt:504)
    at androidx.compose.compiler.plugins.kotlin.lower.ComposerLambdaMemoization.visitNonComposableFunctionExpression(ComposerLambdaMemoization.kt:330)
    at androidx.compose.compiler.plugins.kotlin.lower.ComposerLambdaMemoization.visitFunctionExpression(ComposerLambdaMemoization.kt:365)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunctionExpression(IrElementTransformerVoid.kt:235)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunctionExpression(IrElementTransformerVoid.kt:24)
    at org.jetbrains.kotlin.ir.expressions.impl.IrFunctionExpressionImpl.accept(IrFunctionExpressionImpl.kt:23)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression.transformChildren(IrFunctionAccessExpression.kt:48)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitExpression(IrElementTransformerVoid.kt:131)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitMemberAccess(IrElementTransformerVoid.kt:192)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunctionAccess(IrElementTransformerVoid.kt:195)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:198)
    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:74)
    at org.jetbrains.kotlin.ir.expressions.IrExpression.transform(IrExpression.kt:33)
    at org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression.transformChildren(IrFunctionAccessExpression.kt:48)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitExpression(IrElementTransformerVoid.kt:131)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitMemberAccess(IrElementTransformerVoid.kt:192)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunctionAccess(IrElementTransformerVoid.kt:195)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitCall(IrElementTransformerVoid.kt:198)
    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:74)
    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 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:71)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitDeclaration(IrElementTransformerVoid.kt:57)
    at androidx.compose.compiler.plugins.kotlin.lower.ComposerLambdaMemoization.visitDeclaration(ComposerLambdaMemoization.kt:177)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFunction(IrElementTransformerVoid.kt:69)
    at androidx.compose.compiler.plugins.kotlin.lower.ComposerLambdaMemoization.visitFunction(ComposerLambdaMemoization.kt:218)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitSimpleFunction(IrElementTransformerVoid.kt:72)
    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:28)
    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:70)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitPackageFragment(IrElementTransformerVoid.kt:41)
    at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitFile(IrElementTransformerVoid.kt:47)
    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:62)
    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.ComposerLambdaMemoization.lower(ComposerLambdaMemoization.kt:173)
    at androidx.compose.compiler.plugins.kotlin.ComposeIrGenerationExtension.generate(ComposeIrGenerationExtension.kt:73)
    at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade$doGenerateFiles$1.invoke(JvmBackendFacade.kt:91)
    at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade$doGenerateFiles$1.invoke(JvmBackendFacade.kt:40)
    at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:95)
    at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFiles(JvmBackendFacade.kt:104)
    at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:40)
    at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:647)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:210)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:162)
    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:371)
    at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:105)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:249)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:38)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:80)
    at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:92)
    at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:602)
    at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1644)
    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)

我注释了所有与撰写相关的代码,我重新构建了项目并成功构建。

经过一番挖掘,我发现错误的原因是使用了类似于这行代码中的可点击修饰符 Box( modifier.clickable onClick() )

更新 这是一个要点,其中包含项目的 gradle 文件以及使用的版本https://gist.github.com/DavidIbrahim/effedeebd11efdf554271bb261c31465

【问题讨论】:

你能发布你的应用级build.gradle吗? @SaurabhThorat 添加 确保您对kotlinCompilerExtensionVersion 和您的撰写依赖项使用相同的版本。 很遗憾他们是同一个版本"1.0.0-alpha10" 【参考方案1】:

将compose更新为"1.0.0-alpha11",将Kotlin更新为1.4.21-2后问题解决

【讨论】:

以上是关于使用可点击修饰符时 Jetpack compose 编译错误的主要内容,如果未能解决你的问题,请参考以下文章

Jetpack Compose 从入门到入门

Jetpack Compose 从入门到入门

深度解析 Jetpack Compose 布局

Jetpack Compose中的手势操作

Jetpack Compose布局(一) - 布局基础知识

Jetpack Compose 从入门到入门