Determining the Kotlin version at runtime

To determine the Kotlin version at runtime, Kotlin provides an aptly named KotlinVersion class, which is available since Kotlin 1.1 on all platforms. This class has a few helper method to check that the minimum version of Kotlin has been met.

To use this class, have a look at the following example:

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class CurrentKotlinVersionTest {

    fun kotlinVersionTest() {
        // General information about the current Kotlin version

        // Using the helper method
        assertThat(KotlinVersion.CURRENT.isAtLeast(1, 3)).isTrue()
        assertThat(KotlinVersion.CURRENT.isAtLeast(1, 3, 60)).isTrue()

        // Comparison of Kotlin versions
        assertThat(KotlinVersion.CURRENT > KotlinVersion(1, 2, 71)).isTrue()

The code above uses AssertJ, a fluent Java assertion library, which can be used quite well for Kotlin too.

Leave a Reply

Your email address will not be published. Required fields are marked *