https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
b

Big Chungus

06/14/2022, 1:56 PM
So what's the deal with
androidMain
and
androidTest
sourceSets? They seem to behave differently to other sourceSets somehow. For instance, injecting an intermediate sourceSet doesn't seem to work with this hierarchy:
Copy code
commonMain
  jsMain
  blockingMain
    androidMain
    jvmMain
    linuxX64Main
I have an actual class in
commonMain
which I implement in
jsMain
and
blockingMain
. This works fine for all the targets EXCEPT android, which complains on compilation
e: /path/File.kt: (11, 24): Expected class 'MyClass' has no actual declaration in module <my-module_release> for JVM
I'm adding the dependecies like this
Copy code
kotlin {
  explicitApi()
  android {
    publishLibraryVariants("release", "debug")
  }
  sourceSets {
    afterEvaluate { // Crashes due to missing sourceSet if we don't do this in afterEvaluate
      named("androidMain") {
        dependsOn(getByName("blockingMain"))
      }
      named("androidTest") {
        dependsOn(getByName("blockingTest"))
      }
    }
  }
}
e

ephemient

06/14/2022, 2:12 PM
https://kotlinlang.org/docs/multiplatform-share-on-platforms.html#configure-the-hierarchical-structure-manually
You can have a shared source set for the following combinations of targets:
• JVM + JS + Native
• JVM + Native
• JS + Native
• JVM + JS
• Native
Kotlin doesn't currently support sharing a source set for these combinations:
• Several JVM targets
• JVM + Android targets
• Several JS targets
that
blockingMain
isn't supported
b

Big Chungus

06/14/2022, 2:27 PM
Ah, shit. So I basically need to duplicate my blockingMain code in androidMain?
e

ephemient

06/14/2022, 2:29 PM
I don't know if there are any other workarounds. one that I use in my projects is
Copy code
kotlin {
    sourceSets {
        getByName("jvmMain") {
            kotlin.srcDir("src/commonJvmMain/kotlin")
        }
        getByName("androidMain") {
            kotlin.srcDir("src/commonJvmMain/kotlin"
        }
    }
}
which confuses the IDE but Gradle is fine with it
b

Big Chungus

06/14/2022, 2:30 PM
Gotcha. Thanks for the assist.
s

Sebastian Sellmair [JB]

06/16/2022, 12:57 PM
Jvm and android is not fully supported, but this use can work: use IJ over AS and do not use afterEvaluate: consider using .matching { it.name == } instead
b

Big Chungus

06/16/2022, 4:00 PM
But it
Crashes due to missing sourceSet if we don't do this in afterEvaluate
e

ephemient

06/16/2022, 4:08 PM
Copy code
sourceSets.matching { it.name == "androidMain" }.configureEach { ... }
will work regardless of when sourcesets are registered. but it's very strange that it's needed in the first place;
Copy code
kotlin {
  android()
  sourceSets {
    getByName("androidMain")
    getByName("androidTest")
should work fine, and does for me
b

Big Chungus

06/16/2022, 4:08 PM
you mean matching is evaluated lazily?
2 Views