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

Mike Dawson

10/12/2023, 9:15 AM
The Multiplatform source set hierarchy docs says: Kotlin doesn't currently support sharing a source set for these combinations: ... JVM + Android I've been doing that for a while by creating a "commonJvmMain" or something like that. There are many instances where the dependencies for Android and JVM are the same, as most (but not all) JDK libs are available on Android. The intellisense in IDE often breaks, but it has always compiled and otherwise behaved as expected. Is there a reason for this in the docs? Any hope that this might change?
o

Oliver.O

10/12/2023, 2:25 PM
Actually, KMP is deprecating multiple "same" targets. See here for the rationale and a possible way to restructure your project: • Deprecate multiple ‘same’ targets : KT-59316TargetHierarchy: Support multiple Jvm targets : KT-60173
j

Jeff Lockhart

10/12/2023, 3:11 PM
@Oliver.O JVM and Android are not the same targets though.
o

Oliver.O

10/12/2023, 3:34 PM
Yep. But seemingly close enough for KMP so that the same restrictions apply.
r

russhwolf

10/12/2023, 3:58 PM
I don't think that's true. As far as I'm aware, Android and JVM targets are not the same in the context of KT-59316, and I've built code against 1.9.20 betas that includes shared JVM/Android source sets. What has always been lacking, and what I believe the linked doc refers to, is commonization support. This means that when you have a source set shared between JVM and Android, it can't access JVM APIs even through they're available on both targets.
o

Oliver.O

10/12/2023, 3:58 PM
Additionally, maybe this is helpful when considering possible resolutions: https://github.com/JetBrains/kotlin-web-site/pull/3789/files
@russhwolf OK, got it. So a different cause, similar restrictions.
j

Jeff Lockhart

10/12/2023, 4:04 PM
I have a shared JVM+Android jvmCommon source set in my library and it's definitely a valid use case in some situations. As Russell says, the biggest difference between shared native source sets and shared JVM/Android is that there is no commonization of platform or dependency APIs. Shared JVM+Android source sets can access JVM platform APIs, but it's up to the developer to avoid those that are not also valid Android APIs.
Also, when depending on different library artifacts between JVM and Android, I've run into issues with conflicting APIs being visible to the Android target's source set, as I have the JVM library as a
compileOnly
jvmCommon dependency so the source set can see its APIs, but add the Android library as an implementation dependency in the android source set. This will only be an issue in situations where a library is published as separate platform artifacts, but not when using KMP libraries. (This is how I disambiguate the platform library artifacts, so Gradle removes the inherited JVM dependency, by the way.)
o

Oliver.O

10/12/2023, 4:08 PM
Actually, I had this, and it worked:
j

Jeff Lockhart

10/12/2023, 4:10 PM
That looks like a shared JVM source set between multiple JVM targets, which does fall under the issue you linked, right?
o

Oliver.O

10/12/2023, 4:11 PM
Exactly.
So given the different causes with Android being a special kind of target, would the answer to the OP's question be different?
Any hope that this might change?
j

Jeff Lockhart

10/12/2023, 4:25 PM
@Sebastian Sellmair [JB] has been open to the idea. I would expect it's not the priority right now because it's not in the scope of stable KMP. But hopefully official support can be available in the future, even in the default target hierarchy. 🤞🏼
👍 1
m

Mike Dawson

10/15/2023, 1:40 PM
I understand if this is not in the current scope, it would be interesting to know what is the recommended way to share between Android and JVM in the meantime. In theory I can make a JVM only (non multiplatform) module that builds as a jar, and then add that as a dependency for both Android and JVM. If that module depended on any other multiplatform module it would be a problem because it would then make the Android module depend on JVM modules.
j

Jeff Lockhart

10/15/2023, 5:28 PM
Multiple modules was the recommended approach in this thread, if it works for your project. But yes, there are definitely valid use cases that can't be solved with multiple modules, like my own library with common
expect
and platform `actual`s, where JVM and Android have unique platform-specific dependencies, but otherwise share most of their
actual
implementations.
m

Mike Dawson

10/15/2023, 5:40 PM
Thanks for the link to the thread; I can't help but think it seems strange that I can have a sourceSet for Javascript and JVM, but not one for Android and JVM. My compilations are still working, but it's never a good thing when the docs say it's not supposed to work / isn't supported
90% of the time an Android and JVM implementation are the same. If I copy/pasted from JVM to Android it would work... but that's not exactly best practice
j

Jeff Lockhart

10/15/2023, 5:46 PM
I do feel like the docs deserve to be updated, as the behavior of shared JVM + Android source sets has greatly improved, while the docs haven't changed. Initially at the time of that thread when I first experimented with the jvmCommon source set in my library, there were false positive IDE errors that I had to suppress. But that's no longer the case and for the most part the shared JVM + Android source set mostly just works, with the exception of not having an API commonizer, like native has.
m

Mike Dawson

10/15/2023, 5:48 PM
Is it really just the docs? I used the jvmCommon approach since I started using Kotlin multiplatform in the early days, but the docs explicitly state "Kotlin doesn't currently support sharing a source set for these combinations: Several JVM targets JVM + Android targets" and there is also the issue about deprecating "same targets" : https://youtrack.jetbrains.com/issue/KT-59316/Deprecate-multiple-same-targets
j

Jeff Lockhart

10/15/2023, 6:18 PM
That'd be a question for JB. While there hasn't been any official announcement, it does seem they're open to the idea of supporting JVM+Android. On the other hand, it's clear multiple JVM targets, along with multiple of any target, is not going to be supported. But JVM and Android are unique build targets.
👍 1
s

Sebastian Sellmair [JB]

10/16/2023, 8:30 AM
I am sorry, I was just skimming over this thread. Let me still provide some statement from our (Kotlin team) side: 1. In some future Android and JVM will be indistinguishable from each other from Kotlins perspective (as Android will base on the JVM platform with a new Plugin from Google). 2. Sharing code properly between such jvm based targets is technically challenging (I have given explanations in some of the meetups, but will likely do again on some future meetup) 3. Sharing code between jvm and Android is close to my heart personally as engineer in the Kotlin Team. 4. You can share code between those platforms and it should work reasonably well. I implemented very special code to make it somewhat doable. If you encounter strange issues let me know. Its not a priority but I can work on stuff like this on from time to time. Proper support is scheduled for some time in the future
👍 3
gratitude thank you 2
m

Mike Dawson

10/16/2023, 4:49 PM
Thanks @Sebastian Sellmair [JB] that update is appreciated. My experience (using this over the last four years) is that sharing code between Jvm and Android always compiled as expected, but intellisense in the IDE would normally not recognize JDK libs. Now that also seems to "just work" as expected. The more similar the platform is, the more likely it is that code sharing makes sense. Knowing that is the plan is a bit of a relief. I will leave my commonJvm sourceSets as they are for now.
s

Sebastian Sellmair [JB]

10/16/2023, 5:06 PM
Code highlighting and dependency resolution in the IDE should work for jvm+android source sets. If you see your code not being highlighted, I could offer myself consulting on your buildscripts
m

Mike Dawson

10/16/2023, 5:07 PM
Thanks - these days Jvm/Android shared source sets seem to be highlighted as expected, I'm not seeing any issues anymore.
s

Sebastian Sellmair [JB]

10/16/2023, 5:14 PM
🙇 Glad to hear this. This is how it should be!
👍 1
🙌🏼 1
j

Jeff Lockhart

10/16/2023, 5:27 PM
Any chance the docs could be updated to reflect that JVM + Android isn't completely unsupported, and actually mostly works?
Kotlin doesn't currently support sharing a source set for these combinations:
• Several JVM targets
• JVM + Android targets
• Several JS targets
s

Sebastian Sellmair [JB]

10/17/2023, 11:59 AM
Unfortunately, this a very different discussion. You see: The ‘it works’ is mostly on a ‘best effort’ basis of a single engineer. There is no guarantees attached from the Kotlin Team (only from… me!) So far Kotlin really wants to focus on ios + android (which is totally fair imho). So there is no guarantee on how fast your tickets will be responded to (nor that they will receive fixes)
And explaining this situation on the website is just not feasible as well. Therefore I guess that claiming it being ‘unsupported’ is just the easiest option
Does this sound fair to you?
j

Jeff Lockhart

10/17/2023, 4:09 PM
This makes sense. We're really grateful for your support, Sebastian! Hopefully it can become a priority for the full team in the future. 🤞🏼
🫶 1
m

Mike Dawson

10/18/2023, 7:39 AM
Thanks again Sebastian. I understand the importance of Android + iOS sourceSets, but the compose/Desktop case itself is Android + JVM - a big part of the appeal is the idea that you can take your existing Android code and get a desktop version with minimal additional effort.
s

Sebastian Sellmair [JB]

10/18/2023, 9:01 AM
Fully agree: jvm + Android w/ compose is just such an obvious use case that I would like to use myself!!
👍 2
💯 2
d

darkmoon_uk

11/27/2023, 1:49 AM
Just want to plus1 as a user who's successfully using JVM + Android Targets together. Thanks to your efforts @Sebastian Sellmair [JB] I was happily unaware that this combination is officially unsupported 😬 Considering that Desktop and Android are two major, major targets for cross-platform Applications, I'm glad to see theres a pathway to this becoming officially supported. For there not to be, would be a big miss for KMP 🙏
plus1 1
3 Views