简体   繁体   中英

error: cannot access KMappedMarker When calling kotlin class from Java project

I am migrating a Java android app to Kotlin class for class with Android Studio's Convert Java file to Kotlin file .

My android project consists of three modules/projects where the top project has no code of its own:

overall
 ├─ app
 └─ model

I've converted a file in model to Kotlin and it is recognized by the Java classes in model .But for classes in app there are problems. While the class itself is accepted as eg a method parameter, method references are not possible. Eg for foo.ba() android studio will show a box with the Javadoc but the term in the source file is red underlined and the error is Cannot access kotlin.jvm.internal.markers.KMappedMarker .

To see if the problem is due to Android Studio, I ran gradle --warning-mode all sudoqapp:assembleRelease in the terminal which fails with error: cannot access KMappedMarker and class file for kotlin.jvm.internal.markers.KMappedMarker not found . So I assume the error is in the build.gradle files which were automatically converted.

overall has

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.5.0-RC'
}
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

app has no kotlin references at all and

model has

apply plugin: 'kotlin'
dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
buildscript {
    ext.kotlin_version = '1.5.0-RC'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Adding apply plugin: 'kotlin' to the build.gradle of app as suggested here https://stackoverflow.com/a/50262680 helped.

This is probably not the right thread for my answer, but I stumbled on this thread while debugging the same issue on my Java (Java 17 and Gradle 7.4) project with okHttpClient (written in Kotlin). Maybe this helps to some lost soul like me.

Solution for me was to add requires kotlin.stdlib; in module-info.java file and id 'org.jetbrains.kotlin.jvm' version '1.7.0' in build.gradle file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM