Swapnil Musale
Swapnil Musale

Reputation: 91

Kotlin Multiplatform access file from different sourceSet

I'm working Kotlin Multiplatform library to access device info for platform android, ios, jvm(macOS, linux, windows). I've already done with development for android and ios platform. Now looking to add support for desktop platform.

Note : For simplicity I'm explanning the steps done for macOs platform only.

Below are the steps I've performed :

1.library build.gradle.kts

jvm("desktop") {
    compilations.all {
        kotlinOptions.jvmTarget = "17"
    }
}

listOf(
    macosX64(),
    macosArm64()
).forEach {
    it.binaries.framework {
        baseName = "KDeviceInfo"
        isStatic = true
    }
}

linuxX64 {
    binaries.staticLib {
        baseName = "KDeviceInfo"
    }
}

mingwX64 {
    binaries.staticLib {
        baseName = "KDeviceInfo"
    }
}

2.commonMain

expect class DeviceInfoXState() {
    val androidInfo: AndroidInfo
    val iosInfo: IosInfo
    val desktopInfo: DesktopInfo
}
interface DesktopInfo {
    val macOsInfo: MacOsInfo
}

3.desktopMain

actual class DeviceInfoXState {

    actual val desktopInfo: DesktopInfo
        get() = DesktopInfoImpl()
    actual val androidInfo: AndroidInfo
        get() = throw Exception()
    actual val iosInfo: IosInfo
        get() = throw Exception()
}
class DesktopInfoImpl : DesktopInfo {

    override val macOsInfo: MacOsInfo
        get() = MacOsInfoImpl() // compiler error
}

4.macosMain

class MacOsInfoImpl : MacOsInfo {
 // macos platform specific code   
}

Now I'm facing couple of issue with this implementation :

  1. As I've added macosX64() and macosArm64() compiler ask me to add expect implementation in desktopMain and macosMain as well. If we try to run desktop app on macos system then desktopMain files get called at runtime not macosMain files.
  2. I'm not able to access macosMain files inside desktopMain sourceSet.

Looking for solution/suggestion to tackle with this problem.

Upvotes: 0

Views: 228

Answers (0)

Related Questions