Kotlin Foo::class.java “Unresolved Reference: Java

2019-02-16 06:40发布

问题:

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

How do I get this to work?

回答1:

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



回答2:

It turns out, I was using an older version of Kotlin, and it wasn't configured correctly. I edited the gradle file to include the latest beta version, and selected the option that configures Kotlin, and it works now.

In gradle:

buildscript {
    ext.kotlin_version = '1.0.0-beta-3594'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}
...

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}


回答3:

I Put in the beginning of Gradle (Module app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

and

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

in the dependencies section

In the build.gradle (Project)

buildscript {
    ext.kotlin_version = '1.2.0'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


回答4:

I copied the class from other project and forgot to change the class package name. when I changed, it fixed



回答5:

For easy reference, here are the reflection dependencies when using Gradle:

Reflection libraries from the docs for Gradle in your build.gradle

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "org.jetbrains.kotlin:kotlin-reflect"
    testCompile "org.jetbrains.kotlin:kotlin-test"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}

Reflection libraries syntax for Kotlin Script Gradle DSL in your build.gradle.kts

dependencies {
    compile(kotlin("stdlib"))
    compile(kotlin("reflect"))
    compile(kotlin("test"))
    compile(kotlin("test-junit"))
}


回答6:

I saw this in AndroidStudio with Kotlin 1.2.71 and none of the above fixed it for me.

What sadly hilariously worked for me was closing the project, telling AndroidStudio to forget about the project and re-opening from the folder. Presto, no unresolved reference. Weird, I know.



标签: kotlin