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

kpgalligan

02/22/2019, 6:09 PM
A question for intellij integration. For various target platforms (ios/macos, linux, androidNative), they’re all distinct with regards to “platform” and Native, but when building a library that publishes to these platforms, you’ll really want a common source set for that platform. For example, ios and macos all have “Foundation”, and they would all use it the same way. I define a source set for “apple” that each target’s source set depends on, and that builds fine, but intellij can’t resolve that. Is there a way to configure that to work as expected? I think it would be more like a hint to the IDE that didn’t really impact the compile build (something like “config as if iosArm64"). I’ve done this in the past by having a commented section in the build.gradle that gets uncommented to refresh gradle in Intellij, then commented out again, but that’s pretty ugly.
For example…
// macosX64(“apple”) jvm() js() macosX64() iosArm32() iosArm64() iosX64() mingwX64()
Uncommenting the first line and refreshing intellij will get “import platform.Foundation…” to resolve in the ‘appleMain’ source set. However, expect statements with actuals in there get Intellij errors saying they’re not defined (they compile fine). This is version 2019.1 EAP (Build #IU-191.5701.16)
r

russhwolf

02/22/2019, 6:20 PM
The (hacky) way I've worked around this is by picking one of those sources and making the rest of them depend on it. You'll run into https://youtrack.jetbrains.com/issue/KT-26333 which means you'll see IDE errors on your
expect
declarations, but it will build fine and you'll get IDE inference and autocomplete.
k

kpgalligan

02/22/2019, 6:21 PM
I was thinking that would be the way to do it. If there are open tickets I guess just hack around for now.
r

russhwolf

02/22/2019, 6:27 PM
I don't know of an open ticket for what you're describing (ie inference about shared APIs between different native platforms) but I'd star it if you created one
1
h

hmole

02/22/2019, 7:32 PM
@kpgalligan
Copy code
def emulator = "iosX64"
def iphone = "iosArm64"
kotlin {
  targetFromPreset(presets[emulator], "ios") {

  }
  targetFromPreset(presets[iphone], "iosDevice") {

  }
}

def compilations = kotlin.targets["iosDevice"].compilations
afterEvaluate {
  compilations.named("main").configure { compilation ->
    compilation.source kotlin.sourceSets.findByName("iosMain")
  }
  compilations.named("test").configure { compilation ->
    compilation.source kotlin.sourceSets.findByName("iosTest")
  }
}
j

josephivie

02/22/2019, 7:43 PM
Using `konvenience`:
Copy code
isNative.sources { 
    main {}
    test {}
}
~https://github.com/lightningkite/konvenience~
Ah, never mind. That was a misread. I see what you're asking now - unfortnately I haven't found one.
I'm pretty sure I've seen something for this once. Some way of adding a conditional to the Gradle script that only ran when IntelliJ was analyzing the build, but that otherwise worked for builds.
@kpgalligan I got this to work - use this:
Copy code
if (System.getProperty("idea.resolveSourceSetDependencies") != null) {
    sourceSets {
        maybeCreate("targetSpecificMain").apply {
            kotlin.srcDir("src/somePlatformsMain/kotlin")
        }
    }
}
This feature will be added to Konvenience by default.
b

bdeg

02/23/2019, 8:41 PM
If you're still looking for a solution here's my current solution: https://github.com/bitsydarel/clean-arch/blob/master/coroutines/build.gradle
k

kpgalligan

02/23/2019, 8:46 PM
Interesting. Will take a look
b

bdeg

02/23/2019, 8:47 PM
just check the nativeMain sourceSet
3 Views