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

后端 未结 10 1150
予麋鹿
予麋鹿 2020-12-15 17:11

I am trying to convert my Java code of HomePage.class to Kotlin. I am following the instructions on Kotlin.org:

getClass()

相关标签:
10条回答
  • 2020-12-15 17:25

    i use this

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

    invalided cache & restart android studio!works fine!!

    0 讨论(0)
  • 2020-12-15 17:26

    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"
    }
    
    0 讨论(0)
  • 2020-12-15 17:27

    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.

    0 讨论(0)
  • 2020-12-15 17:28

    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

    0 讨论(0)
  • 2020-12-15 17:29

    Add this line in your Module build gradle

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

    In Project build gradle:

    buildscript {
        ext.kotlin_version = '1.3.21'
        repositories {
            google()
            jcenter()
    
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.3.2'
            classpath 'com.google.gms:google-services:4.2.0'
            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
        }
    }
    
    0 讨论(0)
  • 2020-12-15 17:34

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

    0 讨论(0)
提交回复
热议问题