"Class 'kotlin.Unit' was compiled with an incompatible version" after adding a java class


I use Android Studio 2024.1.2, java 1.8, kotlin 1.9.0, as written in the Studio settings windows.

There is a kotlin project that has been successfully compiled until today, when I decided to add FCM notifications. I have added a MyFirebaseMessagingService class that initially was generated as java class, and not .kt class.

After that I can't build the project due to error:

Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The actual metadata version is 2.1.0, but the compiler version 1.9.0 can read versions up to 2.0.0.
The class is loaded from C:/Users/Administrator/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/2.1.20/aa8ca79cd50578314f6d1180c47cbe14c0fee567/kotlin-stdlib-2.1.20.jar!/kotlin/Unit.class

although I even do not use 2.1.0 version: in the Settings-Other settings - Kotlin compiler it is 1.9.0.

  • Switching to 2.1.0 in settings does not avoid error.

  • I changed java class to kotlin class, and now there are no java files in project - does not change anything.

  • Deleting whole C:/Users/Administrator/.gradle/caches folder does not avoid error - file kotlin-stdlib-2.1.20.jar generates every time I try to build.

  • "Clean project", "Invalidate caches", "Rebuild project" - does not avoid the error.

build.gradle.kts (:app) :

import org.gradle.internal.impldep.bsh.commands.dir
import org.gradle.internal.impldep.org.junit.experimental.categories.Categories.CategoryFilter.include


plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    id("com.google.gms.google-services")
}

android {
    namespace = "com.text.text"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.text.text"
        minSdk = 23
        targetSdk = 36
        versionCode = 52
        versionName = "3.92"


        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
        dataBinding = true
        viewBinding = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.webkit)
    implementation(libs.firebase.messaging)
    implementation(libs.androidx.work.runtime)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

    val navigationVersion = "2.1.0"
    val fragmentVersion = "1.2.0-rc03"

    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation("androidx.legacy:legacy-support-v4:1.0.0")
    implementation("com.google.android.material:material:1.5.0")
    implementation("androidx.constraintlayout:constraintlayout:1.1.3")
    implementation("androidx.navigation:navigation-fragment:$navigationVersion")
    implementation("androidx.fragment:fragment:$fragmentVersion")
    implementation("androidx.fragment:fragment-ktx:$fragmentVersion")
    implementation("androidx.media:media:1.6.0")
    implementation("androidx.credentials:credentials:1.1.1")
    implementation("androidx.credentials:credentials-play-services-auth:1.1.1")
    implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")

    implementation("com.google.android.gms:play-services-auth:21.2.0")

    implementation("com.android.installreferrer:installreferrer:2.2")

   
    implementation("com.google.firebase:firebase-core:21.1.1")
 
    implementation("com.google.firebase:firebase-analytics-ktx:21.4.0")

    implementation("com.facebook.android:facebook-android-sdk:[8,9)")

    implementation(platform("com.google.firebase:firebase-bom:32.0.0"))
    implementation ("com.google.firebase:firebase-messaging")
}

 

I've added two last implementations manually (or at least the last one), and perhaps some were added automatically. Removing two last does not avoid the error.

Another build.gradle.kts

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.kotlin.android)  version "1.9.0" apply false
    id("com.google.gms.google-services") version "4.4.4" apply false

}
0
Feb 14 at 12:24 PM
User Avatarsahgasdvsadgv
#android#kotlin#gradle

Accepted Answer

Fortunately, I had backup of the project and found that the problem was with junit implementation (at least). Anyway, the correct build.gradle.kts :

import org.gradle.internal.impldep.bsh.commands.dir
import org.gradle.internal.impldep.org.junit.experimental.categories.Categories.CategoryFilter.include


plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    id("com.google.gms.google-services")
}

android {
    namespace = "com.text.text"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.text.text"
        minSdk = 23
        targetSdk = 36
        versionCode = 51
        versionName = "2.91"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
        dataBinding = true
        viewBinding = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.webkit)

    //implementation (libs.firebase.messaging)
    //implementation(libs.androidx.work.runtime.ktx)
    //implementation("androidx.work:work-runtime-ktx:2.11.1")
    implementation("androidx.work:work-runtime:2.8.0")
    implementation("androidx.work:work-runtime-ktx:2.8.0")
    //testImplementation(libs.junit)
    //androidTestImplementation(libs.androidx.junit)
    //androidTestImplementation(libs.androidx.espresso.core)
    //androidTestImplementation(platform(libs.androidx.compose.bom))
    //androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

    val navigationVersion = "2.1.0"
    val fragmentVersion = "1.2.0-rc03"

    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation("androidx.legacy:legacy-support-v4:1.0.0")
    implementation("com.google.android.material:material:1.5.0")
    implementation("androidx.constraintlayout:constraintlayout:1.1.3")
    implementation("androidx.navigation:navigation-fragment:$navigationVersion")
    implementation("androidx.fragment:fragment:$fragmentVersion")
    implementation("androidx.fragment:fragment-ktx:$fragmentVersion")
    implementation("androidx.media:media:1.6.0")
    implementation("androidx.credentials:credentials:1.1.1")
    implementation("androidx.credentials:credentials-play-services-auth:1.1.1")
    implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1")

    implementation("com.google.android.gms:play-services-auth:21.2.0")

    implementation("com.android.installreferrer:installreferrer:2.2")

    // Import the BoM for the Firebase platform
    //implementation(platform("com.google.firebase:firebase-bom:30.1.0"))
    implementation("com.google.firebase:firebase-core:21.1.1")
    // Add the dependency for the Analytics library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-analytics-ktx:21.4.0")
    implementation("com.facebook.android:facebook-android-sdk:[8,9)")

    implementation("com.google.firebase:firebase-messaging-ktx:24.1.2")
}
User Avatarsahgasdvsadgv
Feb 14 at 3:49 PM
1