Search code examples
androidkotlin-multiplatformkmm

Kotlin Multiplatform: Can't reference class in commonMain sourceset from another module's androidMain sourceset


I have two multiplatform modules shared and other in a standard multiplatform template project that targets Android and iOS.

shared defines a class in commonMain source set

class SharedGreeting()

other is setup to depend on shared like this in the gradle file:

val commonMain by getting {
            dependencies {
                implementation(project(":shared"))
            }
        }

And in its androidMain sourceset it tries to reference SharedGreeting in some class, fx:

class AndroidGreeter{
   val foo = SharedGreeting()
}

But no matter what I try, I get IDE errors when I try to reference the shared class, and I have to manually add an import statement.

The code compiles and deploys without problems though! Any ideas on what I am missing or misunderstanding? Or is this a bug in KMM?

Full copy of other gradle file:

plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
}

version = "1.0"

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        ios.deploymentTarget = "14.1"
        framework {
            baseName = "other"
        }
    }
    
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(project(":shared"))
            }
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test"))
            }
        }
        val androidMain by getting
        val androidTest by getting
        val iosX64Main by getting
        val iosArm64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosX64Main.dependsOn(this)
            iosArm64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
        }
        val iosX64Test by getting
        val iosArm64Test by getting
        val iosSimulatorArm64Test by getting
        val iosTest by creating {
            dependsOn(commonTest)
            iosX64Test.dependsOn(this)
            iosArm64Test.dependsOn(this)
            iosSimulatorArm64Test.dependsOn(this)
        }
    }
}

android {
    compileSdk = 32
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        minSdk = 24
        targetSdk = 32
    }
}

For full project source code: https://github.com/RabieJradi/kmm_import_error_sample

IDE suggested action for adding a dependency doesn't unfortunately do anything. enter image description here


Solution

  • Turns out it is a bug in KMM and a fix have been implemented by the Jetbrains team here https://youtrack.jetbrains.com/issue/KTIJ-22056/KMM-Cant-reference-shared-module-commonMain-source-set-classes-from-another-modules-androidMain-source-set