Why am I getting Backend Internal error: "Exception during IR lowering error" when using Jetpack Compose clickable-Modifier?

I am creating a custom Checkbox within a Surface which has a Modifier.clickable:

    Surface(
        modifier = Modifier
            .clickable(
                enabled = enabled,
                interactionSource = interactionSource,
                indication = rememberRipple(),
                role = Role.Checkbox,
                onClick = { onCheckedChange(!checked) }
            )
            .then(modifier),
    ) {
        Row {
            Checkbox(checked = checked, onCheckedChange = {}, colors = colors)
            Text(text = text ?: "")
        }
    }

When I try to build that, I get the Exception during IR lowering error error:

org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /home/rene/AndroidStudioProjects/pass13/app/src/main/java/com/aresid/simplepasswordgeneratorapp/ui/widgets/Checkbox.kt

See the full stacktrace here.

Removing the Modifier.clickable solves the build issue.

I already tried up-/downgrading some versions but nothing is working properly.
Currently, I am using those versions:

    ext.versions = [
            'compileSdk': 31,
            'targetSdk' : 30,
            'minSdk'    : 26,
            'kotlin'    : '1.5.30',
            'navigation': '2.3.5',
            'compose'   : '1.0.2'
    ]

Has anybody an idea how to fix that?

Upvotes: 109

Views: 64911

Answers (17)

ugifractal
ugifractal

Reputation: 41

In my case, I fixed the code by changing. Weird? yes, but that's it

import androidx.compose.remember

to

import androidx.compose.runtime.remember

Upvotes: 0

zionpi
zionpi

Reputation: 2671

Firstly ,you should check if your top level project's gradle kotlin version is bigger than 2,if that is the case , you should add the following configuration to your module's gradle file:

plugins {
   id("org.jetbrains.kotlin.plugin.compose") version "2.0.0" // this version matches your Kotlin version
}

the version number should match with your top level kotlin version number.

If your version number is smaller than 2 ,there is no need to do this.

Upvotes: 3

Masum
Masum

Reputation: 4959

In every modules build.gradle.kts if you use @Composable you must have to set these flags in the gradle file.

android {
     .
     .
     .   
     buildFeatures {
         compose = true
     }
     composeOptions {
        kotlinCompilerExtensionVersion = "1.5.3"
    }
}

Upvotes: 2

FarshidABZ
FarshidABZ

Reputation: 4123

If you are using Kotlin 2.0.+, you must use the Compose Compiler Gradle plugin for each module that uses compose.

Define the plugin is following in your libs.versions.toml

[versions]
.....

[plugins]
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

Add the plugin to the project root gradle file

plugins {
   // Existing plugins
   alias(libs.plugins.compose.compiler) apply false
}

Then add this to each module gradle file:

alias(libs.plugins.compose.compiler)

Upvotes: 9

Jack Dickinson
Jack Dickinson

Reputation: 1

I have also encountered a crash with a similar stack trace during compilation after enabling compose in an existing project. The crash was occurring in a file for a fragment that does not use compose, which made it difficult to diagnose since the stack trace was not very useful.

org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering

File being compiled: <File location>

The root cause java. lang AssertionError was thrown at: org. jetbrains.kotlin.ir.util.IrUtilsKt.copyValueParametersToStatic(IrUtils.kt:911)

The cause turned out to be a know issue with jetpack compose compiler which is caused by accessing a protected member of a class in another class that extends from it. This comment gives a more detailed explanation.

There is a proposed solution for this issue but in the meantime, the work around is to remove the protected visibility modifier from these members, which allowed the app to compile without issue.

Upvotes: 0

Cabezas
Cabezas

Reputation: 10717

In my case, I forgot to add in the build.gradle

android {
 ... 

    buildFeatures {
       compose true
    }
    composeOptions {
       kotlinCompilerExtensionVersion compose_version
    }
}

Upvotes: 171

John Wick
John Wick

Reputation: 1

latest update of the compiler should fix this, please check the documentation and the release of 1.5.5 here - https://developer.android.com/jetpack/androidx/releases/compose-compiler#1.5.5

Upvotes: 0

Nikhil Biju
Nikhil Biju

Reputation: 825

In my case, i was trying to add a compose view to a fragment and I got this error what I did to fix it was add compose =true and kotlinCompilerExtensionVersion in build.Gradle

 buildFeatures {
        dataBinding = true
        viewBinding = true
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion= "1.5.3"
    }

Upvotes: 2

thijsonline
thijsonline

Reputation: 1227

As @Sergio pointed out, this error occurs when passing a value to the setContent { } block inside the init { } function of a class. E.g.:

class CustomClass(val variable : String) {

    init {
        someView.setContent {
            someComposeFunction(variable)
        }
    }
}

To workaround this issue, call setContent { } inside another function, and call this function in init { }:

class CustomClass(val variable : String) {

    init {
        setComposeContent(variable)
    }
    
    private fun setComposeContent(val someVariable : String) {

        someView.setContent {
            someComposeFunction(someVariable)
        }
    }
}

Upvotes: 1

T D Nguyen
T D Nguyen

Reputation: 7603

For me, I need to make sure 2 things:

First, that the kotlinCompilerExtensionVersion and other compose version is the same (or compatible - see below link), i.e.

composeOptions {
    kotlinCompilerExtensionVersion "$compose_version"
} 

...
dependencies {

   /**
    * Compose UI layout framework related
    */
   implementation "androidx.compose.ui:ui:$compose_version"
   ...
}

Second, the compose_version is compatible with your current kotlin plugin. Check at this Compose compatibility map

Upvotes: 1

Sumitkumar Dhule
Sumitkumar Dhule

Reputation: 749

Make sure that you have added compose to your gradle, you can add it inside android {...} block. Refer following to add the compose:

buildFeatures {
    compose = true
}

composeOptions {
    kotlinCompilerExtensionVersion compose_version
}

Upvotes: 48

Sergio
Sergio

Reputation: 30645

This build error occurred for me when I was passing some arguments to a Composable function in init block of a class:

class AndroidKeyboardView(context: Context) : FrameLayout(context) {

    init {
        inflate(context, R.layout.keyboard_view, this)
        findViewById<ComposeView>(R.id.compose_view).setContent {
            // The Build Error occurred because of passing the argument. If I remove passing argument `context as IMEService` all works fine.
            KeyboardScreen(context as IMEService) 
        }
    }
}

@Composable
fun KeyboardScreen(connection: IMEService? = null) { ... }

To make it compile without errors a secondary constructor should be used instead of init {} block:

class AndroidKeyboardView(context: Context) : FrameLayout(context) {

    constructor(service: IMEService) : this(service as Context) {
        inflate(service, R.layout.keyboard_view, this)
        findViewById<ComposeView>(R.id.compose_view).setContent {
            KeyboardScreen(connection = service)
        }
    }
}

Upvotes: 2

memres
memres

Reputation: 469

I solved my issue by following steps

  1. Clean project
  2. Delete .gradle and .idea folder
  3. Invalidate Caches/Restart
  4. Sync gradle
  5. Rebuild project

Upvotes: 3

kc_dev
kc_dev

Reputation: 790

This might sound counter-intuitive (after all, the error says "Backend Internal Error"), but for me, restarting the Android Studio client fixed it.

Upvotes: 4

MobileEvangelist
MobileEvangelist

Reputation: 2628

In my case I've created independent module to maintain components and theme independent of functional modules. So updating below in that modules gradle inside android, worked for me.

   android {
.
.
.
         buildFeatures {
                compose true
            }
            composeOptions {
                 kotlinCompilerExtensionVersion '1.0.3'
                 kotlinCompilerVersion '1.5.30'
            }
    }

Upvotes: 11

So I got in touch with the JetBrains team via their issue tracker as @PhilipDukhov suggested and they solved my problem: https://youtrack.jetbrains.com/issue/KT-48815.

I was using kotlinCompilerExtensionVersion = versions.composeVersion in my app's build.gradle file and this is incorrect. versions.composeVersion is something provided by Gradle but it seems to be deprecated. Oneself should manually write the version which they're using there.

Upvotes: 20

Sana Ebadi
Sana Ebadi

Reputation: 7220

its because of JetpackCompose! in gradle 7.2 you should add these lines in build.gradle in android block:

  kotlinOptions {
        jvmTarget = "1.8"
    }
    
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.0.4"
    }

Upvotes: 7

Related Questions