Tests fail just on upgrading Kotlin from 1.4 to 1.5

I have a slightly old but working build.gradle.kts with Gradle 7.1.1 and Kotlin 1.4.21 - all my tests pass when I run gradle test:

import com.jfrog.bintray.gradle.BintrayExtension
import org.jetbrains.dokka.gradle.DokkaTask

plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.4.21"

    // For documentation
    id("org.jetbrains.dokka") version "0.10.1"

    // Apply the java-library plugin for API and implementation separation.
    `java-library`
    // For Maven repository upload
    `maven-publish`
    id("com.jfrog.bintray") version "1.8.4"
}

group = "com.reddove"
version = "0.1.0"

repositories {
    mavenCentral()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    implementation("org.apache.commons:commons-math3:3.6.1")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.21")

    testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
    testImplementation("org.junit.jupiter:junit-jupiter-params:5.4.2")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    //testImplementation("org.jetbrains.kotlin:kotlin-test-junit")

    // additional dependencies used in tests
    testImplementation("org.apache.logging.log4j:log4j-api:2.13.0")
    testImplementation("org.apache.logging.log4j:log4j-core:2.13.0")
}

sourceSets {
    main {
        java {
            srcDir("src/main")
        }
    }
    test {
        java {
            srcDir("src/test")
        }
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
        showStandardStreams = true
    }
}

tasks.withType(Jar::class) {
    manifest {
        attributes["Manifest-Version"] = "1.0"
        attributes["Implementation-Title"] = "Red Dove CFG library"
        attributes["Implementation-Version"] = "0.1.0"
        attributes["Implementation-Vendor"] = "Red Dove Consultants Limited"
        attributes["Implementation-Vendor-Id"] = "com.reddove"
    }
}

tasks.withType(DokkaTask::class) {
    outputFormat = "html"
    outputDirectory = "$buildDir/dokka"
    // add other Dokka configuration here
}

fun prop(s: String) = project.findProperty(s) as String?

publishing {
    publications.register("publication", MavenPublication::class) {
        from(components["java"])
    }

    repositories {
        maven {
            setUrl("https://bintray.com/reddove/public")
            metadataSources {
                gradleMetadata()
            }
        }
    }
}

val publication by publishing.publications

bintray {
    user = prop("bintrayUser")
    key = prop("bintrayAPIKey")
    publish = true
    override = true
    setPublications(publication.name)
    pkg(delegateClosureOf<BintrayExtension.PackageConfig> {
        repo = "public"
        name = "com.reddove.config"
        userOrg = "reddove"
        websiteUrl = "https://www.red-dove.com"
        vcsUrl = "https://github.com/vsajip/ktlib"
        setLabels("configuration")
        setLicenses("BSD-3-Clause")
    })
}

Ignoring the JFrog/bintray stuff which is of course now obsolete, I only changed the Kotlin version from 1.4.21 to 1.5.21 (in two places - the id("org.jetbrains.kotlin.jvm") version "1.4.21" line and the implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.21") line. But now gradle test fails 33 of 35 tests, all because of java.lang.NoClassDefFoundError at various points in my test.kt. What am I doing wrong? The exception is caused here:

java.lang.NoClassDefFoundError: Could not initialize class com.reddove.config.ConfigKt
	at com.reddove.config.Config.<init>(config.kt:2475)

Has something fundamental changed from 1.4 to 1.5 to cause this problem? In config.kt, the offending line is the one which sets up stringConverter:

class Config() {
    // stuff omitted
    var stringConverter: StringConverter = defaultStringConverter

and defaultStringConverter is defined further up the file as

typealias StringConverter = (String, Config) -> Any

val defaultStringConverter = fun(s: String, cfg: Config) : Any {
    // details omitted
}

I’m totally guessing, but it could be because of changed JVM backend in 1.5. Does it help to compile all relevant modules with -Xuse-old-backend compiler argument?

See What's new in Kotlin 1.5.0 | Kotlin

Thanks for the suggestion; it made no difference. I added

tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile> {
  kotlinOptions.useOldBackend = true
}

to build.gradle.kts and updated the Kotlin version to 1.5.21 - it seemed to build OK, as beore, but I get the same result with gradle test :frowning:

Ok, it’s hard to say more without investigation. Can you please create an issue at http://kotl.in/issue with some version of your project to reproduce? BTW visibility of attachment can be restricted to “jetbrains-team”.

I had an exception when switching from 1.51 to 1.52 on a JS project. On JVM and multiplatform platform projects it seems that a similar issue occurs earlier (might be your switch to 1.5 then). I had the exception:

In the log file:
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.Lexer
… 191 more

Different but similar. The solution was then to prioritize the repos to download the plugins differently, since with standard configuration, there seem to be problems from time to time with the POM coming from the gradle repo. You can find some info, how to set up priority for plugin repos here:
https://docs.gradle.org/current/userguide/declaring_repositories.html#sec:plugin-vs-build-repos

Try

pluginManagement {
    repositories {
        mavenCentral()
        maven("https://plugins.gradle.org/m2/")
    }
}

in the settings.gradle.kts

and rebuild the gradle project to given mavenCentral() priority over the gradle repo

this worked for me then. Maybe it might help you

Done.

1 Like