I am trying to convert my Java code of HomePage.class
to Kotlin. I am following the instructions on Kotlin.org:
getClass()
To retrieve the type information from an object, we use the javaClass extension property.
val fooClass = foo.javaClass
Instead of Java’s
Foo.class
useFoo::class.java
.
val fooClass = Foo::class.java
I have a class called HomePage that extends AppCompatActivity (in Android). I am using Android Studio. I tried doing HomePage::class.java
and it has an error: Unresolved reference: java
How do I get this to work?
Once you sync the Gradle build, the error should disappear. If you still see the unresolved reference error after fixing the problem, try to build your Android application with Command + F9 for Mac or Control + F9 for Windows and Linux. The error should disappear after the build is completed.
By using ::class , you get an instance of KClass. It is Kotlin Reflection API, that can handle Kotlin features like properties, data classes, etc. By using ::class. java , you get an instance of Class.
:: converts a Kotlin function into a lambda.
The issue is most likely that you forgot to depend on the reflection libraries which were needed for the reflective functions of Kotlin.
On the Java platform, the runtime component required for using the reflection features is distributed as a separate JAR file (kotlin-reflect.jar). This is done to reduce the required size of the runtime library for applications that do not use reflection features. If you do use reflection, please make sure that the .jar file is added to the classpath of your project.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With