kotlin

How to get a Kotlin KClass from a package class name string?


If I have a string like "mypackage.MyClass", how can I get the corresponding KClass at runtime (from the JVM)?


Solution

  • You can use Java's method of getting a Class instance Class.forName and then convert it to a KClass using the .kotlin extension property. The code then looks like this:

    val kClass = Class.forName("mypackage.MyClass").kotlin
    

    A more direct way may be added at some point. The issue is located here