Is it possible to share a plain jvm code between only android and jvm targets when I have other targets?
g
Giancarlo Buenaflor
05/09/2023, 12:56 PM
You can create a custom
commonJvmMain
target in your source sets and let
androidMain
and
jvmMain
depend on it.
d
darkmoon_uk
05/09/2023, 1:15 PM
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
Javier
05/09/2023, 1:46 PM
shared target doesn’t work with jvm
Javier
05/09/2023, 1:46 PM
you need to add a common folder to android and jvm source sets
j
Jeff Lockhart
05/09/2023, 6:40 PM
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.