Is it possible to share a plain jvm code between o...
# multiplatform
p
Is it possible to share a plain jvm code between only android and jvm targets when I have other targets?
g
You can create a custom
commonJvmMain
target in your source sets and let
androidMain
and
jvmMain
depend on it.
d
Perhaps nit picking but it may help someone to point out this isn't a new 'target'. You never compile for 'common JVM' because no such platform actually exists. Rather, it's an intermediate source set, and in this case models the intersection between two target API's. Another bit of terminology: when you define intermediate source sets like this, you're doing HMPP! (Hierarchical Multi-Platform Programming).
j
shared target doesn’t work with jvm
you need to add a common folder to android and jvm source sets
j
Also good to point out, this intermediate jvm + android source set is technically not officially supported (yet). You may find subtle issues, mostly with what the IDE does. Like it won't commonize JVM and Android dependencies from the downstream source sets (I've worked around this by adding the JVM target's dependency as
compileOnly
in the intermediate source set). JVM platform APIs are available and you can add dependencies directly to the intermediate source set though. Even if there are false positives in the IDE, it should work at compile time of both targets.
g
Yes for sure, thanks for clarifying!