Is there support for having an intermediate source...
# multiplatform
e
Is there support for having an intermediate source set for android and jvm? I remember there is some issue when it comes to android + jvm but I don't remember exactly what it is
e
s
There is preliminary IDE support in the currently released 2022.2.1 preview. Feel free to share feedback.
m
It’s a bit difficult to give feedback on something which does not seem to be documented anywhere or am I just missing it.
s
Yes, this is fair! So let me give more details: We officially do not support sharing code between jvm+android: https://kotlinlang.org/docs/multiplatform-share-on-platforms.html#configure-the-hierarchical-structure-manually There are several key parts missing for ‘proper’ / ‘desired’ support (whatever this means for now). However, this is a famous use-case for compose projects and we therefore maintain a hopefully ‘ok’ ide experience on a ‘best effort’ basis. You can just create a ‘jvmAndAndroidMain’ SourceSet and declare ’androidMain.dependsOn(jvmAndAndroidMain)` as well as
jvmMain.dependsOn(jvmAndAndroidMain)
and you should be able to work inside this SourceSet with IDE support.
This support requires latest IJ preview!
e
So the issue has always been at the IDE level?
s
No, this issue spans from the java/android ecosystem, to the Kotlin Compiler up until the IDE. The IDE, however, can mitigate many issues. Sharing code between android and jvm should work ✅ Creating libraries, sharing code between android and jvm should work ✅ However, the
jvmAndAndroidMain
source set will analyse with jvm only dependencies. This should be fine IMHO, since Android devs are kind-of used to this siutation.
Is it perfect? No Is it absolutely unusable? No How much resources would be necessary to make it better: A LOT! We will come to this, for sure, but we all know other areas that shall be stabilised first, right? 👀 ide import speed 👀 (e.g.)
e
So if we add a dependency to Jetbrains Compose in
jvmAndAndroidMain
would it correctly resolve to the Jetpack Compose artifacts in the Android source set?
o
I don't do Android but the doc quoted above also says: Kotlin doesn't currently support sharing a source set for these combinations: • Several JVM targets However, I'm happily using exactly that (Compose Desktop plus backend JVM). I also do cross-sharing with multiple parents for `frontendJvmMain`:
Copy code
sourceSets {
        val frontendCommonMain by creating {
            dependsOn(commonMain)
            // ...
        }

        val frontendJsMain by getting {
            dependsOn(frontendCommonMain)
            // ...
        }

        val commonJvmMain by creating {
            dependsOn(commonMain)
            // ...
        }

        val frontendJvmMain by getting {
            dependsOn(frontendCommonMain)
            dependsOn(commonJvmMain)
            // ...
        }

        val backendJvmMain by getting {
            dependsOn(commonJvmMain)
            // ...
        }
    }
As I have two executable JVM targets, I'm not using the
application
plugin, which can accommmodate only one. Instead, I'm using use this task configuration:
Copy code
val backendJvmArgs: MutableList<String> = mutableListOf(/* ... */)

tasks {
    val runBackendJvm by creating(JavaExec::class) {
        group = "application"
        mainClass.set("MainKt")
        jvmArgs = backendJvmArgs
        with(kotlin.targets["backendJvm"].compilations["main"] as KotlinJvmCompilation) {
            classpath = output.allOutputs + runtimeDependencyFiles
        }
    }

    val runFrontendJvm by creating(JavaExec::class) {
        group = "application"
        mainClass.set("MainKt")
        with(kotlin.targets["frontendJvm"].compilations["main"] as KotlinJvmCompilation) {
            classpath = output.allOutputs + runtimeDependencyFiles
        }
    }
}
Works like a charm for quite a while now. What problems should I expect?
s
Works like a charm for quite a while now
Hehe, happy to hear this 🎉 I can explain this in the next multiplatform meetup in detail on how stuff like this works rn under the hood and what issues might arise from there!
m
Just to avoid any confusion. We are talking about IJ version
Copy code
IntelliJ IDEA 2022.2.1 Preview (Community Edition)
Build #IC-222.3739.24, built on August 4, 2022
here, right?
o
Over here, it's just IntelliJ IDEA 2022.2 (Ultimate Edition). In German, we have a saying "Aerodynamically, the bumblebee cannot fly. But it doesn't know about that and flies nonetheless." @Sebastian Sellmair [JB] Yes, good idea. Could you please post an announcement about that meetup here? Did not know about it.
m