Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Foo::class.java "Unresolved Reference: Java" error

Tags:

kotlin

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 use Foo::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

enter image description here

How do I get this to work?

like image 365
Rock Lee Avatar asked Dec 07 '15 22:12

Rock Lee


People also ask

How do I fix Kotlin unresolved reference?

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.

What does :: class Java do Kotlin?

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.

What is the meaning of :: In Android Kotlin?

:: converts a Kotlin function into a lambda.


1 Answers

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

like image 195
mhlz Avatar answered Sep 30 '22 20:09

mhlz