Get the versions info in Kotlin runtime

Get the versions info in Kotlin runtime:

Kotlin stdlib comes with one class called KotlinVersion, that provides different properties to check the current Kotlin version runtime.

This class is really useful if you want to test the minimum version of kotlin before running a piece of code.

Example :

fun main() {
    println("Major : ${KotlinVersion.CURRENT.major}")
    println("Minor : ${KotlinVersion.CURRENT.minor}")
    println("Patch : ${KotlinVersion.CURRENT.patch}")

    println("Version : ${KotlinVersion.CURRENT}")

    println("Is at least 1.1 ${KotlinVersion.CURRENT.isAtLeast(1, 1)}")
    println("Is at least 1.1.1 ${KotlinVersion.CURRENT.isAtLeast(1, 1, 1)}")

    val supportedVersion = KotlinVersion(1, 1, 1)

    if (KotlinVersion.CURRENT > supportedVersion) {
        println("Supported..")
    }
}

Explanation :

Here :

  • KotlinVersion.CURRENT is the current version of kotlin library.

  • major, minor and patch are the components of this version.

  • isAtLeast method is to check if a version is at least another version.

  • Similarly, we can create one KotlinVersion object and compare it with any other object. The first argument is the major, second argument is the minor and third argument is the patch components.

It will print one output like below :

Major : 1
Minor : 3
Patch : 70
Version : 1.3.70
Is at least 1.1 true
Is at least 1.1.1 true
Supported..

Similar tutorials :