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

后端 未结 10 1151
予麋鹿
予麋鹿 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:35

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

    In my case reducing version of kotlin fixed the issue

    Change

    ext.kotlin_version = '1.3.21'
    

    To

    ext.kotlin_version = '1.3.11'
    
    0 讨论(0)
  • 2020-12-15 17:51

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

    I used

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

    Instead of

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    
    0 讨论(0)
提交回复
热议问题