Noob question about multi modules and compose andr...
# compose-desktop
c
Noob question about multi modules and compose android and desktop. I've worked with compose on android and desktop very little and I've never worked with a multi module android app. In this case though... I have a fairly simple existing app that I already added compose to, but my plans are to add a new module to my android app that houses compose called
composables-common
I already created this module in Android Studio as a new android library module. Now I want to create a new module in the same project that is a compose desktop module and have it depend on
composables-common
Two questions: 1. Is making my composables-common module supposed to be a
com.android.library
module? 2. Does anyone see any issue with my "proposed" setup? Existing android app module. Create new composables-common android library module. Set this composables-common as a dependency in my existing app module. Create new compose desktop app plain ol java module. Depend on composables-common dependency in the desktop module. Working on Android? Open the project in AS and choose android app module to launch. Working on desktop? Open the project in IJ and choose desktop app module to launch. I have seen the sample projects showing desktop and android app in a single repo, but I guess I'm more stuck on how to do it for an already existing project. Thank you!
i
To be able to use
composable-common
in
desktop
module, both
composable-common
and
desktop
should be MPP modules (i.e you should apply
kotlin("multiplatform")
in build.gradle). Your common code in
composable-common
should also be in
commonMain
instead of
jvmMain
or
main
So your steps will be: 1. create
composables-common
in Android Studio 2. convert this module to MPP (apply MPP plugin, move sources to
commonMain
).
com.android.library
should stay 3. Create
desktop
MPP module (not plain java, because this is not properly supported by IDE yet).
android
module can remain as a plain android module, not as MPP. 4. depend `desktop` module on
composable-common
5. depend `android` module on
composable-common
P.S. Probably there is another option - not to create separate modules
desktop
/
android
. And just keep the single MPP module
app
with
desktopMain
and
androidMain
sourcesets. But I haven't tried this 🙂
c
Thank you @Igor Demin I will try this at some point today/this week. The only thing that I don't know how to do is "Create 
desktop
 MPP module". Would I just open my project directory in IJ and create a module from there? Or should I try to add the module from android studio?
i
The best bug free option is to copy-paste the desktop module from here and register this new module here
c
Thanks. I hope I will have time to try it out today. I'm very excited. Thank you so much for your detailed steps. One last question I suppose. Will I open the project in Android studio for app module development and open it in IntelliJ for desktop module development?
i
Android Studio supports desktop modules as well. In terms of supporting pure Java / MPP modules it is almost the same as IDEA Community edition. Although the Ultimate edition supports more features (such as Async Profiler)
💯 1
c
Tried today for a bit. Failed at step 2: 😭
convert this module to MPP (apply MPP plugin, move sources to 
commonMain
). 
com.android.library
 should stay
Tried to convert my module to an MPP module. I think it's because by default AS creates modules with build.gradle instead of build.gradle.kts and when I try to do that... everything seems to just stop working in the IDE. Syntax highlighting, auto completion, etc.
i
There are two options: • convert your entire project to Gradle Kotlin DSL before adding a new module • figure out how to use groovy with Compose for Desktop. It is theoretically possible, but I didn't try and didn't find any examples. Oh. I also forget one big step. If you use
androidx.compose
, you have to switch your project to `org.jetbrains.compose`: 0. Apply plugin
id("org.jetbrains.compose")
instead of compose true. Remove all explicit dependencies on
androidx.compose
And instead of creating a new module
composable-common
and converting it to MPP it is also easier to just copy it from here
c
Thanks. I just added the module like you said (copy pasted the directory) and I at least don't have build errors on that module anymore. After the copy paste, trying to add another composable to App.kt doesn't auto-complete. Migrating my android apps build.gradle file to build.gradle.kts is a little worrysome as I was under the impression that kts support wasn't fully there yet.
Also... just for laughs... @Igor Demin I tried opening up the multiplatform-template directory in android studio and even just trying to open that results in errors.
i
Are there any errors in Studio after the Gradle sync?
c
No errors from gradle or anything. Maybe the template wasn't made to be used with latest Studio canary. I think I have some actionable items ahead of me: 1. Convert my existing project to kts scripts. I've been putting this off because I remember hearing that studio did not support kts well but maybe now is the time. 2. I will file a bug for opening up the multiplatform-template in latest AS canary showing that it doesn't work. 3. Last... I will likely wait for some of the whole setup into an existing project to be a little more straightforward. Copy pasting a common module and desktop module is 100% fine for me, but I'm assuming that my root build.gradle and my android build.gradle are likely messing something up because I'm using compose dependencies, compose true, etc. Maybe there's a guide missing on how to first update your existing android app from "android compose" to "jetbrains compose".
@Igor Demin one last thing. It seems that now modifying my root build.gradle clears up most errors, but if I try to run the desktop app in studio it doesn't launch. Any ideas?
message has been deleted
Clicking the green play icon in the gutter created this run configuration... but it's "greyed" out
message has been deleted
i
Running with a green play icon isn't supported yet. Try to run as a Gradle task:
c
Weird my task list doesn't show any tasks. Only "dependencies" which I don't ever remember seeing in the gradle window?
message has been deleted
And desktop doesn't even have a dropdown. Going to try to invalidate and restart IDE.
So close to being able to launch a desktop app from my "current" android-only project, but yeah. Something with my gradle tasks doesn't want to work. I'm going to do those 3 things I said above. 1. Upgrade to kts 2. Bug report for multi platform template 3. Look for documentation on how to go multi platform in an existing app (dos and donts of app module build.gradle) and I'll most likely try again in Kotlin 1.5 Thanks again for your help @Igor Demin looking forward to the day where I can get this set up... but for now it's exceeded my time box I allocated for this.
297 Views