40

I'm trying to use the 1.4.21-2 version of kotlin which is a recent version that has a fix that allows you to use Compose + Kotlin serialization without the build hanging. This is all great, however, the Compose compiler does not know about it and gives the following (rather unhelpful) error:

e: This version (1.0.0-alpha09) of the Compose Compiler requires Kotlin version 1.4.21 but you appear to be using Kotlin version 1.4.21-2 which is not known to be compatible. Please fix your configuration (or suppressKotlinVersionCompatibilityCheck but don't say I didn't warn you!).

I would love to provide that suppression flag, however I don't know where to put it... I've spent about an hour trying to put it in random places in my gradle files, for example in composeOptions, but no luck. I also tried all the google-fu I know, but nobody seems to have actually used this and wrote anything about it.

Any ideas how to get out of that predicament?

Alexandru Cristescu
  • 3,898
  • 3
  • 19
  • 22

7 Answers7

31

I have the same problem with message:

e: This version (1.0.0-alpha11) of the Compose Compiler requires Kotlin version 1.4.21-2 but you appear to be using Kotlin version 1.4.21 which is not known to be compatible.  Please fix your configuration (or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).

Adding compiler args solved my problem:

"-P", "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"

You can add it to all KotlinCompile tasks. In app level Gradle it looks like the following:

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs += [
                "-Xallow-jvm-ir-dependencies",
                "-P",
                "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
    }
}
Pedro Massango
  • 4,114
  • 2
  • 28
  • 48
karenkov_id
  • 630
  • 5
  • 8
29

if you are using KTS,

android {
    ...

    kotlinOptions {
        jvmTarget = "1.8"
        useIR = true

        freeCompilerArgs += listOf(
            "-P",
            "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        )

    ...
}
Louis CAD
  • 10,965
  • 2
  • 39
  • 58
sonique
  • 4,539
  • 2
  • 30
  • 39
  • 6
    I'm trying to get compose 1.1.1 to work with kotlin 1.6.20. Compose only works with 1.6.10 without suppressing this version check. If I use the supplied code, I have to remove useIR = true because now it is declared as val instead of var. You can't change it. After removing the useIR line, I get an error stating NoSuchMethodError "e: java.lang.NoSuchMethodError: org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl". Anybody else having this issue? – TALE Apr 15 '22 at 20:44
13

For multi-module projects add the suppression flag to project's root-level build.gradle inside allprojects {} section:

allprojects {
    repositories {
        google()
        mavenCentral()
    }

    // Suppress Compose Kotlin compiler compatibility warning
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            freeCompilerArgs += [
                    "-P",
                    "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
            ]
        }
    }
}
HumbleBee
  • 1,212
  • 13
  • 20
8

It is a command line argument.

See this example in a kts file here

-P plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Calin
  • 6,661
  • 7
  • 49
  • 80
7

A better solution is to add a composeOptions section to your :app module build.gradle file.

    composeOptions {
        kotlinCompilerExtensionVersion = "1.3.2"
    }

This is what is recommended in the Android Compose Kotlin Documentation

dustmachine
  • 10,622
  • 5
  • 26
  • 28
3

If you are using Gradle Grovy script (build.gradle),

    kotlinOptions {
        jvmTarget = "1.8"
        useIR = true

        freeCompilerArgs += [
                "-P",
                "plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
        ]
    }
Bao Le
  • 16,643
  • 9
  • 65
  • 68
  • 2
    Doesn't seem to work for me `Could not set unknown property 'useIR' for object of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl.` If I remove that, I get: `java.lang.NoSuchMethodError: 'void org.jetbrains.kotlin.psi2ir.generators.TypeTranslatorImpl.(org.jetbrains.kotlin.ir.util.ReferenceSymbolTable, org.jetbrains.kotlin.config.LanguageVersionSettings, org.jetbrains.kotlin.descriptors.ModuleDescriptor, kotlin.jvm.functions.Function0, boolean, org.jetbrains.kotlin.ir.util.StubGeneratorExtensions, int, kotlin.jvm.internal.DefaultConstructorMarker)'` – m0skit0 Jun 17 '22 at 09:33
  • Seems like Compose really needs that Kotlin version then. – helios Sep 22 '22 at 20:26
1

You can also always simply downgrade the Kotlin Gradle Plugin to the version it states, i.e:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    val compose_version by extra("1.0.0-beta07")

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:7.0.0-beta02")
        
        // This is the line you want to change to the needed version
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32")
    }
}
luckyhandler
  • 10,651
  • 3
  • 47
  • 64