Hello. Can’t import Compose classes in `iosMain` s...
# multiplatform
p
Hello. Can’t import Compose classes in
iosMain
sourceset when using
expect/actual
with
@Composable
(but not always). IDE says that everything is imported correclty, but when building the code,
compileIosMainKotlinMetadata
task fails with the following:
Copy code
Unresolved reference: Composable
Unresolved reference: Composable
Functions which invoke @Composable functions must be marked with the @Composable annotation
Mismatched @Composable annotation between expect and actual declaration
@Composable invocations can only happen from the context of a @Composable function
This happened very unpredictably. For some time everything used to build fine, but then some files were moved to other modules and this appears, and I have NO idea even how to investigate why this could be happening. I also couldn’t reproduce it in a kmp-wizard sample. Here’s the gradle config from this module (I used multiple convention-plugins initially, but rewrote it in one file)
Copy code
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
    id("com.android.library")
    kotlin("multiplatform")
    id("org.jetbrains.compose")
}

kotlin {
    applyDefaultHierarchyTemplate()

    jvm("desktop")
    androidTarget()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    @OptIn(ExperimentalKotlinGradlePluginApi::class)
    compilerOptions {
        freeCompilerArgs.add("-Xexpect-actual-classes")
    }

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(projects.common.core.design)
                implementation(projects.common.core.presentation)
                implementation(compose.runtime)
                implementation(compose.foundation)
                implementation(compose.material3)
                implementation(compose.ui)
                implementation(compose.components.uiToolingPreview)
                implementation(libs.compose.cupertino.adaptive)
            }
        }
        val desktopMain by getting {
            dependencies {
                implementation(compose.desktop.common)
                implementation(compose.desktop.currentOs)
            }
        }
        val androidMain by getting {
            dependencies {
                implementation(compose.preview)
            }
        }
        val nonIosMain by creating {
            dependsOn(commonMain)
            androidMain.dependsOn(this)
            desktopMain.dependsOn(this)
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
    }
}

android {
    namespace = "org.example"

    compileSdk = properties["target.sdk"].toString().toInt()

    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    sourceSets["main"].res.srcDirs(
        "src/androidMain/res",
        "src/commonMain/resources",
        "build/generated/libres/android/resources",
    )
    sourceSets["main"].resources.srcDirs("src/commonMain/resources")

    defaultConfig {
        minSdk = properties["min.sdk"].toString().toInt()
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = libs.versions.android.compose.compiler.version.get()
    }
    dependencies {
        debugImplementation(compose.uiTooling)
    }
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions.jvmTarget = "17"
}
P.S. Needless to say, cleaning /build folder, invalidating caches, clearing .gradle/caches, updating Android Studio, reinstalling Xcode, upgrading AGP, Gradle, compose versions didn’t change a thing
a
Hi 👋 Defining both `applyDefaultHierarchyTemplate()`and
Copy code
val nonIosMain by creating {
            dependsOn(commonMain)
            androidMain.dependsOn(this)
            desktopMain.dependsOn(this)
        }
looks suspicious to me...
a
hi @Pavel S were you able to resolve this ? i can run the iosApp but failing for
Copy code
./gradlew build
p
As far as I remember, I fixed it by not using iosMain sourceset and copy pasting the same code in iosX64Main, iosArm64Main and iosSimulatorArm64Main instead
👌 1