Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 644
Sabinmon ks Avatar asked Aug 27 '20 15:08

Sabinmon ks


People also ask

Can you use Java with jetpack compose?

Can I use it with Java? Jetpack Compose is Kotlin exclusive. It uses features such as coroutines, and the handling of @Composable annotations is done by a Kotlin compiler. There is no way to get access to these from Java.

Is jetpack compose ready for production?

This 1.0 release is ready for use in production, offering key features that you need: Interoperable: Compose is built to interoperate with your existing app. You can embed compose UIs within Views or Views within Compose.

Is jetpack compose reactive?

Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language. It is fully declarative, meaning you describe your UI by calling a series of functions that transform data into a UI hierarchy.

What is composable in jetpack compose?

Jetpack Compose is built around composable functions. These functions let you define your app's UI programmatically by describing how it should look and providing data dependencies, rather than focusing on the process of the UI's construction (initializing an element, attaching it to a parent, etc.).


4 Answers

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"]
    }
}
like image 166
Blunderer Avatar answered Oct 12 '22 23:10

Blunderer


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.

like image 27
Bryan Avatar answered Oct 12 '22 23:10

Bryan


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.

like image 40
j0h1_r Avatar answered Oct 12 '22 23:10

j0h1_r


.kts version

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
    kotlinOptions.freeCompilerArgs = listOf(
        *kotlinOptions.freeCompilerArgs.toTypedArray(),
        "-Xallow-jvm-ir-dependencies",
        "-Xskip-prerelease-check")
    useIR = true
}
like image 39
Mahdi Safarmohammadloo Avatar answered Oct 12 '22 22:10

Mahdi Safarmohammadloo