Pavel S
06/12/2024, 2:05 PMiosMain
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:
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)
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 thingAdam S
06/12/2024, 2:29 PMval nonIosMain by creating {
dependsOn(commonMain)
androidMain.dependsOn(this)
desktopMain.dependsOn(this)
}
looks suspicious to me...Adam S
06/12/2024, 2:33 PMAhmed na
10/12/2024, 8:51 AM./gradlew build
Pavel S
10/12/2024, 9:59 AM