Expected class has no actual declaration
Asked Answered
F

3

8

I am doing a multiplatform project.

A part of my gradle file looks like this

...
kotlin {
    jvm()
    jvm("api")
    js()
    mingwX64("mingw")
    sourceSets {
        ...
        val jvmMain by getting {
            dependencies {
                implementation ("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
            }
        }
        val jvmTest by getting {
            dependencies {
                implementation ("org.jetbrains.kotlin:kotlin-test")
                implementation ("org.jetbrains.kotlin:kotlin-test-junit")
            }
        }
        val apiMain by getting {
            dependencies {
                dependsOn(jvmMain)
            }
        }
        val jsMain by getting {
            dependencies {
                implementation ("org.jetbrains.kotlin:kotlin-stdlib-js")
            }
        }
        ...
    }
}

Now in the commonsMain sources I get an IDE error (red underline) that says "expected class Sample has no actual declaration in apiMain", but apiMain depends on jvmMain which has the actual declaration.

I don't think I need an actual declaration in apiMain since I already have one in jvm.

Should I take a different approach in setting-up my source sets?

Anyway the project compiles fine and I can run apiMain, but the IDE error really annoys me.

Femur answered 23/2, 2019 at 4:25 Comment(1)
youtrack.jetbrains.com/issue/KT-29433 could be related. youtrack.jetbrains.com/issues/KT is the proper place for such issue reports.Elemental
C
15

This is a known IDE bug. See YouTrack ticket here.

For now, you can use @Suppress("NO_ACTUAL_FOR_EXPECT") to get rid of the warning in your IDE. I wouldn't really recommend it though, because you'll get a runtime crash instead of a compile-time error if you're missing any actual implementations.

Cornelia answered 23/2, 2019 at 17:34 Comment(1)
I guess, I'd have to live with it until it's fixed. ThanksFemur
S
2

They should be under the same package name.

Also check if your package name is the same as the one you get the error

expected class Sample has no actual declaration in apiMain

You need to have the package apiMain.

In my case I had enter image description here And I had to create both folders in resources enter image description here

Scrubber answered 22/10, 2021 at 11:59 Comment(0)
S
2

In my case, it turns out that something in my ".idea" folder had gotten corrupted. I closed Android Studio, deleted that directory from the root of my project and then reopened the project. The .idea file was automatically re-created and my problem was gone.

I figured this out because creating a new KMM app using the wizard didn't have the same error that I was seeing with expect/actual classes.

Suki answered 22/9, 2022 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.