【Compose】First try of Desktop Application

foreword

As an android developer for many years, since the release of Jetpack, the Compose component has attracted me deeply. I used the new version of android studio to create a template last year, but I didn't invest much time and energy. In fact, There is another news that I didn't pay much attention to, that is, Jetbrains, the parent company of kotlin, also launched Compose Multiplatform (also called compose-jb) at the end of last year, and it is still a stable version. This made me feel suddenly enlightened. My layout is still not open. I can’t just treat it as a UI component in a jetpack toolkit. It turns out that its goal is cross-platform, and it doesn’t blow or black. If I’m on android The code written above (kotlin) can be reused on the PC side, and some modules of the app can be easily transplanted (such as the current WeChat desktop side), which will be a long-cherished wish for many years. Kotlin+compose will try to move from mobile Counter-attack PC (remember to write a desktop program, I am not very good at C++, I try to learn Qt, what swing? Hey, don't mention it).

Of course, Flutter can do it a long time ago. I have to say that you must know Dart first. If you are an android developer, I think you must be learning or using kotlin, and even started to use Compose to build UI. Yes, it all worked out without a hitch.

compose-jb: Compose Multiplatform, including the following three:

  • compose-android:即 Jetpack Compose
  • compose-desktop:即 Compose for Desktop
  • compose-web:即 Compose for Web
  • compose-ios: Compose for ios (not yet, I heard there is action)

Prepare

Come experience creating and publishing on the desktop today. Environmental requirements:

  • IntelliJ IDEA 2020.3 or higher.
  • Compilation requires JDK 11 or higher, and packaging release requires JDK 15 or higher.

Download URL: https://jdk.java.net/archive/
Download URL: https://mirrors.huaweicloud.com/openjdk/18.0.2/

create project

insert image description here
Create wait for gradle sync. If you want to preview the interface, search for the compose entry function in setting->Plugins
insert image description here
is main() in Main.kt, and the style is also Material, full of Google flavor.
insert image description here

Pack

The packaging format can be chosen arbitrarily. Configured in build.gradle.kts in the root directory. Select tasks in the sidebar, double-click to execute, my host is window, so try msi.

enum class TargetFormat(
    internal val id: String,
    internal val targetOS: OS
) {
    
    
    AppImage("app-image", currentOS),
    Deb("deb", OS.Linux),
    Rpm("rpm", OS.Linux),
    Dmg("dmg", OS.MacOS),
    Pkg("pkg", OS.MacOS),
    Exe("exe", OS.Windows),
    Msi("msi", OS.Windows);

insert image description here
For the first packaging, the packaging tool wix3 will be downloaded. If the network speed is not good, you can download it manually and extract it to the project directory \build\wixToolset. Download address: wix311-binaries.zip

Successful compilation output:

build\compose\binaries\main\msi\untitled-1.0.0.msi

install and run

insert image description here

The dependent lib and runtime are in the installation directory, execute the exe.
insert image description here
Click the button, there will be the unique water ripple effect on android
insert image description here

question

One, if there is the following prompt, please configure to ensure that your JDK is greater than or equal to 15.

Packaging native distributions requires JDK runtime version >= 15
Actual version: ‘11’
Java home: C:\Program Files\Android\Android Studio\jre

Second, modify the environment variables and remember to restart the IDE. Make sure jdk takes effect.

insert image description here
Three, the package exe can succeed, but the single operation fails, and there is no response directly. Friends who know the reason can leave a message to guide.

Summarize

There is no best, only the most suitable. I hope that the popularity of kotlin in android will allow more people to choose compose. In this way, compared with choosing flutter to learn a new language, traditional android developers can make steady progress. Exercise in jetpack compose, if it is more useful and popular, I believe it can make the application development field that has been cold in recent years, and the future employment opportunities will be wider.

References

Build better apps faster with Jetpack Compose

Compose Multiplatform

https://github.com/JetBrains/compose-jb

Jetpack Compose for Desktop: Milestone 1 released

10 questions to help you understand Compose cross-platform

Replacing the traditional Java UI framework, JetBrains Kotlin declarative UI framework Compose Multiplatform 1.0 is officially launched

Compose Multiplatform for PC, Web and Android enters Alpha

This article quickly takes you to understand the status quo of KMM, Compose and Flutter

The latest version of the Jetpack compose development and application guide (Compose underlying principles, strengthening actual combat)

Use ComposeDesktop to develop a desktop-side multifunctional APK tool

Jetpack Compose Desktop Desktop version packaging and publishing application

Introduction to Jetpack Compose for Desktop and use

Jetpack Compose for Desktop to package and generate Windows Msi installation package and exe executable program exited with 311 code

Guess you like

Origin blog.csdn.net/lucky_tom/article/details/128036111
Recommended