【问题标题】:Couldn't inline method call 'remember' into @androidx.compose.runtime.Composable无法将方法调用“记住”内联到 @androidx.compose.runtime.Composable
【发布时间】:2021-08-12 15:06:03
【问题描述】:

我有一个大项目,我决定向其中添加 jetpack compose。首先,我准备了一个包含一些 @Composable 组件的独立项目,并且一切正常。然后,在编译期间向我的项目添加源和准备依赖项后,我开始收到此错误:

org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: C:/Users/.../CatalogScreen.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
    at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
    ...
Caused by: java.lang.RuntimeException: Exception while generating code for:
FUN name:CatalogScreen visibility:public modality:FINAL <> () returnType:kotlin.Unit
  annotations:
    Composable
  BLOCK_BODY
    VAR PROPERTY_DELEGATE name:currentTab$delegate type:androidx.compose.runtime.MutableState<kotlin.Int> [val]
      CALL 'public final fun remember <T> (calculation: @[DisallowComposableCalls] kotlin.Function0<T of androidx.compose.runtime.ComposablesKt.remember>): T of androidx.compose.runtime.ComposablesKt.remember [inline] declared in androidx.compose.runtime.ComposablesKt' type=androidx.compose.runtime.MutableState<kotlin.Int> origin=null
        <T>: androidx.compose.runtime.MutableState<kotlin.Int>
        calculation: BLOCK type=kotlin.Function0<androidx.compose.runtime.MutableState<kotlin.Int>> origin=LAMBDA
          COMPOSITE type=kotlin.Unit origin=null
          FUNCTION_REFERENCE 'private final fun CatalogScreen$lambda-0 (): androidx.compose.runtime.MutableState<kotlin.Int> declared in ...CatalogScreenKt' type=kotlin.Function0<androidx.compose.runtime.MutableState<kotlin.Int>> origin=LAMBDA reflectionTarget=null

    at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
    at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate$default(FunctionCodegen.kt:43)
    at org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen.generateMethodNode(ClassCodegen.kt:349)
    ...
Caused by: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call 'remember' into
@androidx.compose.runtime.Composable public fun CatalogScreen(): kotlin.Unit defined in ...catalog
<no source>
Cause: Not generated
File is unknown
The root cause java.lang.IllegalStateException was thrown at: org.jetbrains.kotlin.codegen.inline.InlineCodegen$Companion.getCompiledMethodNodeInner(InlineCodegen.kt:578)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen.throwCompilationException(InlineCodegen.kt:101)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen.performInline(InlineCodegen.kt:141)
    at org.jetbrains.kotlin.backend.jvm.codegen.IrInlineCodegen.genInlineCall(IrInlineCodegen.kt:148)
    at org.jetbrains.kotlin.backend.jvm.codegen.IrInlineCallGenerator$DefaultImpls.genCall(IrInlineCallGenerator.kt:29)
    ...
Caused by: java.lang.IllegalStateException: Couldn't obtain compiled function body for IrBasedSimpleFunctionDescriptor: FUN IR_EXTERNAL_DECLARATION_STUB name:remember visibility:public modality:FINAL <T> (calculation:@[DisallowComposableCalls] kotlin.Function0<T of androidx.compose.runtime.ComposablesKt.remember>) returnType:T of androidx.compose.runtime.ComposablesKt.remember [inline]
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen$Companion.getCompiledMethodNodeInner(InlineCodegen.kt:578)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen$Companion.access$getCompiledMethodNodeInner(InlineCodegen.kt:542)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen.createInlineMethodNode$backend(InlineCodegen.kt:535)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen.performInline(InlineCodegen.kt:134)
    ... 70 more

我的简单代码如下所示:

@Composable
fun CatalogScreen() {

    var currentTab by remember { mutableStateOf(0) }

}

在我的项目中,我使用了 compose 1.0.1、kotlin 1.5.21、gradle 7.1.1 以及许多插件和库,包括协程和 kotlinx 序列化,但是很难删除每个依赖项并不能使代码正常工作。

也许有人遇到过这种错误并且知道什么会导致它或什么会破坏它?

【问题讨论】:

  • 如果在将依赖项添加到工作项目后开始发生这种情况,请使用二进制搜索来检测它,例如首先注释一半等,当您检测到问题时将其报告给 compose issue tracker 或特定的依赖维护者,或两者兼而有之
  • 我很难说这是否与编译步骤或构建配置有关。我不确定,为什么我的解决方案有效,但是......它仍然有效:)

标签: android kotlin android-jetpack-compose


【解决方案1】:

您的模块的 build.gradle 文件中也可能缺少此内容

buildFeatures { // Enables Jetpack Compose for this module
    compose = true
}
composeOptions {
    kotlinCompilerVersion versions.kotlin
    kotlinCompilerExtensionVersion versions.composeCompiler
}

【讨论】:

    【解决方案2】:

    解决方案非常棘手:

    在我的buildSrc buildGradle 中,我有以下代码:

    repositories {
        google()
        jcenter()
        maven(url = "https://storage.googleapis.com/r8-releases/raw")
    }
    
    plugins {
        `kotlin-dsl`
    }
    
    dependencies {
        implementation("com.android.tools.build:gradle:7.0.0")
    }
    

    api(kotlin("gradle-plugin:1.5.21")) 添加到依赖项解决了构建问题。

    我将把这条线索留给会影响这个问题的其他开发者。

    【讨论】:

    • 这也解决了我的问题。但我不确定为什么。我正在更改 buildSrc 中的内容,但为什么我需要 buildSrc gradle 文件中的依赖项才能使其在单独的模块中工作?
    • 成功了。 implementation 而不是 api 对我来说已经足够了。
    【解决方案3】:

    我听说每当出现“内部后端编译器错误”时,它都是由编译器本身引起的,应该报告。尝试从 Studio Templates 创建一个新项目,看看它是否可以编译。如果是这样,那么只需将您的代码复制粘贴到该代码中即可。

    谢谢!

    【讨论】:

    • 当然,您附加的 sn-p 没有错误,前提是您的项目中已经存在正确的依赖项和导入,正如您在原始问题中提到的那样。
    • 感谢您的建议,解决方案很简单,但很难发明。我不知道这是连接到编译器还是构建配置
    • 嘿,请看看这是不是你的情况。 developer.android.com/jetpack/compose/gestures#nested-scrolling
    • 不要像在构建文件中那样显式添加依赖项,只需查看settings.gradle 并在那里修改版本即可。似乎是 Bumblebee Canary 6 中的一个错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多