So what's the deal with `androidMain` and `android...
# multiplatform
b
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
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
Ah, shit. So I basically need to duplicate my blockingMain code in androidMain?
e
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
Gotcha. Thanks for the assist.
s
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
But it
Crashes due to missing sourceSet if we don't do this in afterEvaluate
e
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
you mean matching is evaluated lazily?