https://kotlinlang.org logo
Title
d

Daniele B

06/17/2021, 11:08 PM
I am trying to investigate the following way for defining multiplatform composables: • creating a new project called “*composables*” as multiplatform, with 3 sourcesets: ◦ commonMain (with composables used by all) ◦ materialMain (with composables used by Android and Desktop), as JVM ◦ webMain (with composables just used by Web), as JS However, I am getting some errors, as I guess I have missed something (maybe a lot) in my gradle file definition. Does this approach make any sense to you?
If setup properly, in theory I shouldn’t get errors in the “MaterialMain” shared by Android and Desktop
What I can see it’s missing in the gradle file is a reference to the Android and Desktop Compose version. How should I specify that?
This is my project structure. Beside the “composables” project, I also have: • androidApp • desktopApp • webApp The Compose version is already specified on those projects. I guess I need to connect somehow these projects to the “composable” project.
a

Arkadii Ivanov

06/17/2021, 11:24 PM
You should also enable the compose plugin:
org.jetbrains.compose
. And I'm not sure if Android will work with plain JVM module with Compose, but worth to try.
d

Daniele B

06/17/2021, 11:33 PM
Thanks @Arkadii Ivanov I have just changed the “*composables*” gradle file to this. But I still get the same errors:
r

rnett

06/17/2021, 11:47 PM
Are you actually adding the compose dependencies anywhere? You didn't share your source set config. Because the target setup looks correct
d

Daniele B

06/17/2021, 11:50 PM
@rnett do you mean the
SourceSets
on this gradle file? I didn’t add any, as these composable as not dependent on any libraries. They are pure Compose.
r

rnett

06/17/2021, 11:51 PM
It does depend on compose though. Afaik the compose plugin doesn't automatically add runtime dependencies
d

Daniele B

06/17/2021, 11:55 PM
Yes, it seems so. Currently the Compose dependencies are specified on the app projects gradle files. I wonder if: • I need to duplicate the same compose dependencies to this “composables” gradle file • I should specify the compose dependencies ONLY on this “composables” gradle file (by removing them from the app projects gradle files) • I should connect the app projects to this “composables” project (and how)
r

rnett

06/18/2021, 12:13 AM
I would depend on the compose runtime as
api
in composables, since your app projects are depending on it, right? They might need some platform specific dependencies, but the common ones should be as high up in the dependency tree as possible
d

Daniele B

06/18/2021, 12:17 AM
not sure how the dependency on the compose runtime should be specified
r

rnett

06/18/2021, 12:36 AM
See https://kotlinlang.org/docs/mpp-add-dependencies.html, you probably want it in common (I think it supports it? not sure) I know there's some multiplatform compose projects floating around, you might want to start with one of them
d

Daniele B

06/18/2021, 12:41 AM
I am confused. Not sure which Compose dependencies should go in Common.
r

rnett

06/18/2021, 1:38 AM
I think you have to trial and error a bit, but https://github.com/JetBrains/compose-jb/tree/master/examples/falling_balls_with_web is a good starting point
https://github.com/JetBrains/compose-jb/tree/master/examples/todoapp looks like it has a web module as well, with a different setup
d

darkmoon_uk

06/18/2021, 4:32 AM
@Daniele B I have got this setup working and would be happy to assist. It sounds like part of your issues may be a confusion that also caught me for a bit; that you should be using
implementation(compose.web.widgets)
from your
common
source-set. Yes, the 'web'-named artifact is
common
😱 🤦‍♂️ I called this out last week to [JB] as being misleading.
Here's the
build.gradle.kts
from my
client
module, featuring (what I similarly called) a
materialCompose
source-set.
a

Arkadii Ivanov

06/18/2021, 7:16 AM
From the very recent web artifacts are also published together with normal desktop and Android. So it should be possible to use
api(compose.runtime)
. Just double check that the version you are using is published with web.
0.5.0-build225
should work!
Only the
runtime
dependency will be possible in common, others are not published for Web at the moment.
j

Javier

06/18/2021, 7:35 AM
Compose Gradle plugin has the compose accessor for getting dependencies, but it is not applying dependencies, but you need to apply then in
commonMain
(which should be used in all targets), and the specific deps in the specific source sets (maybe navigation in Android, and so on).