23

I have a big project and I decided to add jetpack compose to it. First, I prepared a standalone project with some @Composable components, and everything was working. Then, after adding sources and preper dependencies to my project during compilation I started receiving this error:

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

My simple code looks like this:

@Composable
fun CatalogScreen() {

    var currentTab by remember { mutableStateOf(0) }

}

In my project I'm using compose 1.0.1, kotlin 1.5.21, gradle 7.1.1 and lot's of plugins and libraries including coroutines and kotlinx serialization, however ugly removing of each dependency doesn't make code working.

Maybe someone met with this kind of error and know what can lead to it or what can break it?

2
  • 1
    If this started happening after adding dependencies into working project, detect it using binary search, like comment half at first, etc. when you've detected you problem report it to compose issue tracker or to that particular dependency maintainers, or both Commented Aug 13, 2021 at 2:25
  • This is hard for me to say if this relates to compiling step or with build configuration. I'm not sure, why my solution works, but... it still works :) Commented Aug 13, 2021 at 12:22

4 Answers 4

62

You might also be missing this from your module's build.gradle file

buildFeatures { // Enables Jetpack Compose for this module
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion versions.composeCompiler
}
Sign up to request clarification or add additional context in comments.

1 Comment

kotlinCompilerVersion is deprecated and can be removed. Compose now uses the kotlin compiler.
12

Solution was very tricky:

In my buildSrc buildGradle I had following code:

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")
}

Adding api(kotlin("gradle-plugin:1.5.21")) to dependencies solved build problems.

I'm leaving this clue for other devs who will affect this issue.

3 Comments

This fixed my issue as well. But I'm not sure why. I was changing stuff in buildSrc but why would I need the dependency in the buildSrc gradle file in order for it to work in the separate modules?
It worked. And implementation instead of api was enough for me.
As per issuetracker.google.com/issues/176079157#comment14, this is intended behaviour.
1

I've heard that whenever there's 'an internal backend compiler error', it is originating from the compiler itself, and should be reported. Try creating a new project from Studio Templates and see if it compiles. If it does, then just copy paste your code in that one.

Thanks!

4 Comments

and of course, there's no error in the snippet you've attached, provided the correct dependencies and imports are already in your project, as you've mentioned too, in your original question.
Thanks for your suggestion, solution was easy but super hard to invent. I don't know if this is connected to compilator or rather build configuration
Instead of adding the dependency explicitly like that in the build file, just check out the settings.gradle and modify the version over there. Seems to be a bug in Bumblebee Canary 6
-2

I am sorry if I am a bit late!

Just check your import statement for remember

Replace import androidx.compose.remember with import androidx.compose.runtime.remember and you're done!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.