Cannot find "Greeting" in scope

Viewed 1036

I've build a KMM project in Android Studio but the ios app seems to not see the classes from the shared module. Initially it worked perfectly but now that I modified some classes in the shared module, the error appears.

My project structure looks this way:project structure

As you can see there are some compilation errors in the Platform class but the android app is working fine. The error says "The feature "multi platform projects" is experimental and should be enabled explicitly".

The ios app looks like this:

ios app

Later Edit:

So apparently I found that I encounter this errors when I add this dependencies in shared.build.gradle in the common source set:

        val commonMain by getting {
            dependencies {
//                implementation("io.ktor:ktor-client-core:$ktorVersion")
//                implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
//                implementation("io.ktor:ktor-client-json:$ktorVersion")
//                implementation("io.ktor:ktor-client-serialization-jvm:$ktorVersion")
            }
        }

I need these dependencies to make requests to my ktor server.

Update: So the problem was that I added some libraries in the common set and those libraries did not have multiplatform support.

4 Answers

I had created a new KMM default app and selected the cocoapods option. From Android Studio, I opened the iosApp.xcodeproj - this is why I got the error.

Instead, I had to open the iosApp.xcworkspace. By additionally running cd iosApp && pod install I was able to build and run the app from Xcode.

open Xcode from Android Studio

You may have to cd iosApp && pod install the first time you build the iOS app.

If the Xcode errors are just showing up in source, but you aren't actually trying to build it, you'll need to try an "build" the project. That will trigger Kotlin to run, which will generate the declaration for Greeting in the shared header, and swift can see that.

Essentially, when you change Kotlin, Xcode can't see those changes right away, so you'll get an error if you write the code.

I'm am assuming that "initially it worked perfectly" is accurate, in which case you probably just need to rerun the build. The part about The feature "multi platform projects" is experimental and should be enabled explicitly is a message I've seen, but I assumed was a warning. I don't think that needs to be enabled anywhere, unless maybe you have warnings configured to be treated as errors. I haven't set up a new project in a while, though, so maybe something gets set by copy/paste that I'm not aware of.

So the problem was that I added some libraries in the common set and those libraries did not have multiplatform support. Should have paid more attention.

Related