Search code examples
reflectionkotlin

Determine version of Kotlin at runtime


What can be written to determine the version of Kotlin at runtime?

fun main(args : Array<String>) { 
  println("v" + System.getProperty("java.version"))
}

prints a version, but it is the Java JDK version. Using "kotlin.version" prints null. Can this be done at runtime?

If the general answer is no, is there a way to embed this information from the compile phase into a particular function or class?


Solution

  • Use kotlin.KotlinVersion.CURRENT:

    fun main(args: Array<String>) {
        println(KotlinVersion.CURRENT)
    }
    

    Try it out here by changing the version in the bottom-right corner of the page.