Sabinmon ks
Sabinmon ks

Reputation: 635

When trying jetpack compose it show error: compiler backend and cannot be loaded by the old compiler

Class 'androidx.compose.ui.platform.ComposeView' is compiled by a new Kotlin compiler backend and cannot be loaded by the old compiler

This my onCreate method :

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_delivered, container, false).apply {
        findViewById<ComposeView>(R.id.compose_view).setContent {
            MaterialTheme {
                Surface {
                    Text("Hello")
                }
            }
        }
    }
}

Compose version:

accompanistVersion = "0.1.9"
composeVersion = '0.1.0-dev17'

app.gradle

buildFeatures {
        compose true
        dataBinding true
    }
    composeOptions {
        kotlinCompilerVersion rootProject.kotlinVersion
        kotlinCompilerExtensionVersion rootProject.composeVersion
    }



// Compose
    implementation "androidx.compose.runtime:runtime:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation:$rootProject.composeVersion"
    implementation "androidx.compose.foundation:foundation-layout:$rootProject.composeVersion"
    implementation "androidx.compose.material:material:$rootProject.composeVersion"
    implementation "androidx.compose.ui:ui-viewbinding:$rootProject.composeVersion"
    implementation "androidx.ui:ui-tooling:$rootProject.composeVersion"
    implementation "androidx.compose.runtime:runtime-livedata:$rootProject.composeVersion"
    implementation "com.google.android.material:compose-theme-adapter:$rootProject.composeVersion"
    implementation "dev.chrisbanes.accompanist:accompanist-coil:$rootProject.accompanistVersion"

Upvotes: 25

Views: 8325

Answers (6)

Shrikant
Shrikant

Reputation: 363

find kotlin compatible version here

Update dependency in build.gradle

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${version}"

here ${version} is kotlin version.

Configure a suitable JDK version in Gradle Setting. enter image description here

Run below commands

gradle clean

gradle build

For me, it resolved my issue.

Please let me know if it doesn't work for you.

Upvotes: 0

This is the dependency which causes respective error in my case:

androidTestImplementation 'androidx.compose.ui:ui-test-junit4:1.0.5'

My guess that playing with it should help

Upvotes: 0

Mahdi Safarmohammadloo
Mahdi Safarmohammadloo

Reputation: 553

.kts version

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs = listOf(
        *kotlinOptions.freeCompilerArgs.toTypedArray(),
        "-Xallow-jvm-ir-dependencies",
        "-Xskip-prerelease-check")
    useIR = true
}

Upvotes: 1

Bryan
Bryan

Reputation: 15135

The default app/build.grade, when creating a new project with an "Empty Compose Activity", includes the following options.

android {
    // other options

    kotlinOptions {
        jvmTarget = '1.8'
        useIR = true
    }

    // more options
}

Adding these options (specifically useIR = true) seemed to fix the error for me.


The useIR option references a new JVM backend for Kotlin for which the documentation specifically states the following.

If you enable Jetpack Compose, you will automatically be opted in to the new JVM backend without needing to specify the compiler option in kotlinOptions.

Which is seemingly incorrect.

Upvotes: 20

Blunderer
Blunderer

Reputation: 994

Please add this task in your app/build.gradle file:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs += ["-Xallow-jvm-ir-dependencies", "-Xskip-prerelease-check"]
    }
}

Upvotes: 19

j0h1_r
j0h1_r

Reputation: 61

Following the steps in the official setup guide lead me to the same problem.

Adding the necessary dependencies/configuration for the compose library fixed this issue for me.

Upvotes: 4

Related Questions