Jetpack Compose for the Desktop!03 Nov 2020 Share on:
There have been indications in recent times of changes being made that would allow use of Jetpack Compose to develop UI for desktop apps. That became closer to reality today when it was noticed that EAP version of IntelliJ IDE has option now to create projects that target use of Compose on desktop JVM platforms (Windows, Linux, macOS). In this article I’m going to outline initial exploration of using this capability in PeopleInSpace repo. Changes made are included in this branch.
(Update Nov 5th 2020: Official announcement for Jetpack Compose for Desktop: Milestone 1 Released)
The existing PeopleInSpace project currently has dependency on Android Studio 4.2 Canary version so for purposes of this experiement I removed the
app module (and related Android code) from the project and created a new
compose module for the desktop Compose code. It was then mostly a matter of copying in
build.gradle.kts from the sample IntelliJ project in to this module. That code was then updated to include following (with
PersonList and other Compose code being almost identical to that already used in Android app.). Note also that the same
Kotlin Multiplatform shared code that was invoked from Android Jetpack Compose code (and also other platforms) was used here to retrieve data shown in the UI.
With those changes it was then just a matter of running
main.kt) resulting then in following UI. One other important thing to note here is that you need to udpate IntelliJ run configuration to use
bundled JRE (it has also been pointed out that selecting Java 11 should also work.)
Running from command line
To allow running from command line (using
java -jar) I’ve also updated
compose module to include following
Having done that you can now run
./gradlew :compose:uberJar to create the jar file and then following to run from command line
(Update Nov 5th 2020)
With official Milestone release it turns out that Compose gradle plugin supports creating installers/packages for the supported platforms:
- macOS — .dmg, .pkg
- Windows — .exe, .msi
- Linux — .deb, .rpm
Also shout out to Adam Bennett for following tweet (and related thread) for inspiring this experiment and also importantly highlighting some of steps required.