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"
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With