Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose: Backend Internal error when calling function with lambda from Coroutine

Sort of strange one but was getting "Backend Internal error" error when using Jetpack Compose and turned out it was triggered by calling a function that takes a lambda from within a Coroutine.

It's pretty easy to reproduce....have narrowed it down to following steps:

Create new project using "Empty Compose Activity" template. Update to dev07 (had issue with previous versions as well) and also add following to build.gradle

composeOptions {
    kotlinCompilerExtensionVersion = "0.1.0-dev07"
}

Add following to MyActivity.kt

fun someFun(success: (String) -> Unit) {
}

and then update onCreate to following:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    lifecycleScope.launch {
        someFun {
        }
    }
    setContent {
        MaterialTheme {
            Greeting("Android")
        }
    }
}

Build project and then you see following error:

e: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
Element is unknownThe root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:42)
    at org.jetbrains.kotlin.codegen.CompilationErrorHandler.lambda$static$0(CompilationErrorHandler.java:35)
    at org.jetbrains.kotlin.backend.jvm.JvmBackendFacade.doGenerateFilesInternal$backend_jvm(JvmBackendFacade.kt:114)

Note you also need to add following dependency to build.gradle

implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
like image 373
John O'Reilly Avatar asked Dec 01 '25 01:12

John O'Reilly


2 Answers

Make sure to add this to every module you use compose in

buildFeatures {
    compose true
}

This was a comment by alashow and the only thing that worked.

like image 141
Harsh P Avatar answered Dec 04 '25 18:12

Harsh P


Make sure that you have added compose to your Gradle inside android {...} block. You have to do it for every module that uses compose. Refer following to add the compose:

buildFeatures {
    compose = true
}

composeOptions {
    kotlinCompilerExtensionVersion compose_version
}
like image 40
Sumitkumar Dhule Avatar answered Dec 04 '25 16:12

Sumitkumar Dhule