56

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?

René Jörg Spies
  • 1,544
  • 1
  • 10
  • 29
  • compose `1.0.2` doesn't support kotlin `1.5.30`, it requires `1.5.21`. If you need to use `1.5.30`, you can switch to alpha `1.1.0-alpha03`. – Phil Dukhov Sep 18 '21 at 13:39
  • @PhilipDukhov Thanks for your answer. I do not require Kotlin version 1.5.30 but downgrading to 1.5.21 results in the exact same error unfortunately. See the full stacktrace here https://pastebin.com/qXpyF8p4 – René Jörg Spies Sep 19 '21 at 10:57
  • It's hard to say what exactly causes the error, your code builds fine to me. First of all make sure you're using up to date AS, which is Arctic Fox | 2020.3.1 Patch 2. If you're already using it, `BackendException` means a bug of compose compiler or kotlin backend, I suggest you reporting it to both [compose issue tracker](https://issuetracker.google.com/issues/new?component=612128) and [JetBrains issue tracker](https://youtrack.jetbrains.com/newIssue), including full project and version of your Android Studio so it can be easily reproduced – Phil Dukhov Sep 19 '21 at 11:06
  • 1
    @PhilipDukhov Thanks again. Android Studio is up-to-date. I have created bugs in each tracker you sent me. Lets hope for the best! – René Jörg Spies Sep 19 '21 at 17:01
  • @RenéJörgSpies I am getting a similar issue... can you provide a link to the issue – MRX Oct 02 '21 at 13:26
  • @MRX Sure, check my answer below – René Jörg Spies Oct 02 '21 at 22:11

10 Answers10

73

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

android {
 ... 

    buildFeatures {
       compose true
    }
    composeOptions {
       kotlinCompilerExtensionVersion '1.0.3'
       kotlinCompilerVersion '1.5.30'
    }
}
Cabezas
  • 9,329
  • 7
  • 67
  • 69
21

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
}
Sumitkumar Dhule
  • 409
  • 5
  • 10
16

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.

René Jörg Spies
  • 1,544
  • 1
  • 10
  • 29
10

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'
            }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36
  • Why is kotlinCompilerVersion for? Using just kotlinCompilerExtensionVersion and all compiles without issues. – David Jun 30 '23 at 00:07
6

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"
    }
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
  • 1
    Why do you need to set jvmTarget? Without it, it also builds in my case, so I'm not sure if add it or not. – David Jun 30 '23 at 00:07
4

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

kc_dev
  • 578
  • 1
  • 6
  • 21
  • 1
    Awesome, this worked for my team. The error happened randomly for a file that didn't use Compose (the module did and was configured correctly). Thank you! – Paul T. Jan 11 '23 at 15:04
2

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
memres
  • 439
  • 2
  • 9
  • This worked for me when all the other answers failed, so it's an upvote from me! For reference, I was getting it when I started to set a priority order on my compose test rule, to solve a different issue. i.e. `@get:Rule(order = 1) val composeTestRule = createComposeRule()`. Following the above steps 'fixed' it. – xavierdominguez Aug 17 '22 at 15:23
2

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)
        }
    }
}
Sergio
  • 27,326
  • 8
  • 128
  • 149
1

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

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
0

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)
        }
    }
}
thijsonline
  • 1,048
  • 11
  • 15