https://kotlinlang.org logo
Title
u

ursus

02/19/2023, 3:56 PM
Is it possible to consume a pure kotlin dependency in a multiplatform module?
val commonMain by getting {
    dependencies {
        api(project(":some-pure-kotlin-stuff"))
    }
}
gives me odd errors at sync time
:shared:iosArm64Main: Could not resolve project :some-pure-kotlin-stuff
Required by:
    project :shared

Possible solution:
 - Declare repository providing the artifact, see the documentation at <https://docs.gradle.org/current/userguide/declaring_repositories.html>
Is this not supported? Can KMP modules only depend on KMP modules?
m

Matt Nelson

02/19/2023, 4:02 PM
The dependency must support the targets which you have enabled in your module. From the looks of your error, the "pure kotlin" dependency does not support the
iosArm64
target and cannot provide the
.klib
for it.
u

ursus

02/19/2023, 4:03 PM
I don't follow, the pure kotlin dependency is just a
plugins {
    id "kotlin"
}
and nothing extra. Should that not just work?
m

Matt Nelson

02/19/2023, 4:04 PM
use
kotlin("multiplatform")
u

ursus

02/19/2023, 4:04 PM
is that the only way? to make the pure kotlin multiplatform aware? I thought if there is no jvm/android dependency anywhere, it can be consumed directly
m

Matt Nelson

02/19/2023, 4:06 PM
👀 I'm not sure of that... Would be nice.
r

russhwolf

02/19/2023, 4:11 PM
At the moment there's no such thing as a "pure Kotlin" module the way you seem to be expecting. Multiplatform modules must declare their targets, and can only depend on modules that include the same targets. Using
id "kotlin"
isn't pure Kotlin, it's actually JVM. You need to use the multiplatform gradle plugin for multiplatform.
u

ursus

02/19/2023, 4:15 PM
I see, so
id "kotlin"
is essentially equivalent to
plugins {
    id "org.jetbrains.kotlin.multiplatform"
}

kotlin {
    jvm()
}
?
j

jw

02/19/2023, 4:15 PM
no it's an alias for
org.jetbrains.kotlin.jvm
it behaves similarly to what you wrote, but not strictly the same
u

ursus

02/19/2023, 4:18 PM
I see, I somehow thought the final app can dictate the target platform, but yea that's nonsense, "id kotlin" is jvm, invokes kotlinc, which produces jvm bytecode etc
j

jw

02/19/2023, 4:19 PM
What you are referring to is IR-only dependencies which are still a long way away. This will allow you to ship target-agnostic Kotlin which is lowered to the specific platforms you are targeting as part of your compilation step. It's also not 100% bulletproof, as a new target may be incompatible with the APIs you reference.
u

ursus

02/19/2023, 4:21 PM
is that dependant on the K2 coming online?
j

jw

02/19/2023, 4:22 PM
It depends on a stable, serializable IR format. I don't think that's strictly tied to K2, but they certainly will do K2 before doing this.
u

ursus

02/19/2023, 4:23 PM
I see, that would be sweet, although, gradle folks recommend the convention plugins anways, so hiding the setup shouldn't be that hard
r

russhwolf

02/19/2023, 4:23 PM
https://youtrack.jetbrains.com/issue/KT-52666 if you want to follow progress
u

ursus

02/19/2023, 4:23 PM
so in a "pure" kotlin module (lets just say some business logic, no platform dependencies, just kotlin sdtlib)
plugins {
    id "org.jetbrains.kotlin.multiplatform"
}

kotlin {
    jvm()
    ios()
}
is the minimum config needed to get android and ios apps going?
j

jw

02/19/2023, 4:24 PM
ios()
does not include the arm simulator, but otherwise yes. also you may want
android()
and not
jvm()
depending on whether you use Android APIs or not.
u

ursus

02/19/2023, 4:28 PM
when setting up our project some time ago, I was peripherally aware of kotlin multiplatform, hoping it would mature, and came up with internal rule to have pure jvm kotlin modules whenever practical, which is turns out most of the app, and naively I think this makes built times faster 😄
j

jw

02/19/2023, 4:28 PM
it absolutely does
a pure JVM module will run like three tasks to build. an Android one runs like 100.
u

ursus

02/19/2023, 4:29 PM
okay so the MO should be jvm+ios(+simulator), and we can sort of pretend it's a IR-only dependency in
/commonMain
r

russhwolf

02/19/2023, 4:30 PM
well, until you decide you want to add browser support 🙂
u

ursus

02/19/2023, 4:32 PM
as in JS?
r

russhwolf

02/19/2023, 4:32 PM
yeah, or wasm eventually
I wouldn't worry too much about that though if it's not already on your roadmap. It's impossible to future-proof everything
u

ursus

02/19/2023, 4:34 PM
we have a monorepo, adding it should be trivial, given the internal convention plugin
thank you all, very enlightening!
to clarify, by spraying
plugins {
    id "org.jetbrains.kotlin.multiplatform"
}

kotlin {
    jvm()
    ios()
}
everywhere, I will double my
assemble
time, right?