https://kotlinlang.org logo
b

Benjamin Deroche

06/29/2022, 10:04 AM
I have a KMM project that target Android, iOS and desktop JVM. Is there any way to easily share a small part of code between Android and JVM? It's just 20 lines of code, it doesn't look enough to create a new separate module just for this 😔
v

vanniktech

06/29/2022, 10:18 AM
I know I starred an issue for an intermediate source set just like iosMain, but I can't seem to find it. Your best option is to maybe expect/actual and just throw on iOS
b

Benjamin Deroche

06/29/2022, 11:30 AM
But then the "actual" in androidMain and desktopMain will still have duplicated code
v

vanniktech

06/29/2022, 11:32 AM
The platform relevant bits right, if that's almost all the code, then a module sounds better
r

ribesg

06/29/2022, 2:01 PM
I have modules with like 2 lines of code
r

russhwolf

06/29/2022, 6:10 PM
You can do a shared android/jvm source set, but the IDE support is inconsistent. If it's only a small amount of code that might not be too big an issue.
b

Benjamin Deroche

06/30/2022, 7:21 AM
@russhwolf That sound amazing, how does it work?
r

russhwolf

06/30/2022, 12:06 PM
Something like this
Copy code
android()
jvm()
sourceSets {
    val androidMain by getting {}
    val jvmMain by getting {}
    val jvmCommonMain by creating {
        androidMain.dependsOn(this)
        jvmMain.dependsOn(this)
        dependsOn(commonMain)
    }
}
You may still need to manually write some copy/paste expect/actual declarations for dependencies that the Kotlin tooling doesn't correctly recognize are accessible to both android and jvm.
v

vanniktech

07/04/2022, 10:25 AM
That's the issue I've been looking for: https://youtrack.jetbrains.com/issue/KT-42466
2 Views