Android studio 4.1, Kotlin 1.4.20-RC, incorrect import syntax error display

廉价感情. 提交于 2020-12-15 04:14:33

问题


Android Studio 4.1 (AS)

In a kotlin project, with a module that has android(), jvm(), and ios() multiplatform targets, I have an expected class in commonMain sourceset which has actual class implementations in androidMain and in jvmMain. The androidMain actual class shows an error in AS on the java imports:

import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.Currency

The "java" in each line is red, as if the IDE can't see the dependency that resolves this. So of course all the related source using those imports shows in the IDE as errors also. The errors shown by the IDE are wrong, as the required dependencies are in the build.gradle.kts for the androidMain sourceset and for the jvmMain sourceset, and they resolve just fine during gradle builds.

Interestingly, the actual class in jvmMain sourseset with the exact same source in it as the one in AndroidMain, shows no errors in AS. So AS is only showing the incorrect syntax errors in the androidMain sourceset.

I've only seen this on kotlin multiplatform modules. Anyone seen this and know how to fix? I don't know what the IDE does that might make it resolve gradle dependencies incorrectly.

Thanks in advance for any help/info.

Here's the gradle for the module in question:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform")
    id("com.android.library")
    id(SrcLibraries.spotless)
}

val moduleName = "monayCommon"
group = App.appId
version = App.appVersionName

kotlin {
    jvm()
    android()
    iosX64("ios") {
        binaries {
            framework {
                baseName = moduleName
            }
        }
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(project(":ioCommon"))
                implementation(kotlin("stdlib-common"))
                implementation("com.oldguy.kmpsc:kmp-sc:0.5.0-SNAPSHOT")
                implementation(SrcLibraries.klock)
                implementation(SrcLibraries.bigDecimal)
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val androidMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
                implementation(SrcLibraries.androidxCoreKtx)
            }
        }
        val androidTest by getting {
            dependsOn(commonMain)
            dependsOn(androidMain)
            dependencies {
                implementation(Kotest.junit5)
                implementation(Kotest.junit5Runtime)
                implementation(Kotest.core)
                implementation(Kotest.assertions)
            }
        }
        val jvmMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation(kotlin("stdlib-jdk8"))
            }
        }
        val jvmTest by getting {
            dependsOn(commonMain)
            dependsOn(jvmMain)
            dependencies {
                implementation(Kotest.junit5)
                implementation(Kotest.junit5Runtime)
                implementation(Kotest.core)
                implementation(Kotest.assertions)
            }

        }
        val iosMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation(kotlin("stdlib"))
            }
        }
        val iosTest by getting {
            dependsOn(commonMain)
            dependsOn(iosMain)
        }
        all {
            languageSettings.useExperimentalAnnotation("kotlin.ExperimentalUnsignedTypes")
        }
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

android {
    compileSdkVersion(AndroidSDK.compile)
    buildToolsVersion = AndroidSDK.buildToolsVersion
    defaultConfig {
        minSdkVersion(AndroidSDK.min)
        targetSdkVersion(AndroidSDK.compile)
        versionCode = App.appVersion
        versionName = App.appVersionName
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }

    packagingOptions.exclude("META-INF/LICENSE.md")
    packagingOptions.exclude("META-INF/LICENSE-notice.md")

    sourceSets {
        getByName("androidTest") {
            setRoot("src\\androidAndroidTest")
            manifest.srcFile("src\\androidMain\\AndroidManifest.xml")
            java.srcDirs("src\\androidAndroidTest\\kotlin")
        }
        getByName("test") {
            setRoot("src\\androidTest")
            java.srcDirs("src\\androidTest\\kotlin")
        }
        getByName("main") {
            setRoot("src\\androidMain")
            manifest.srcFile("src\\androidMain\\AndroidManifest.xml")
            java.srcDirs("src\\androidMain\\kotlin")
        }
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

val packForXcode by tasks.creating(Sync::class) {
    group = "build"
    val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
    val framework = kotlin.targets.getByName<KotlinNativeTarget>("ios").binaries.getFramework(mode)
    inputs.property("mode", mode)
    dependsOn(framework.linkTask)
    val targetDir = File(buildDir, "xcode-frameworks")
    from({ framework.outputDirectory })
    into(targetDir)
}

tasks.getByName("build").dependsOn(packForXcode)

回答1:


This was resolved by adding

kotlin.mpp.enableGranularSourceSetsMetadata=true

to gradle.properties



来源:https://stackoverflow.com/questions/64838634/android-studio-4-1-kotlin-1-4-20-rc-incorrect-import-syntax-error-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!