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

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


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"
}

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"

or

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$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
    }
}

Tags:

Kotlin