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
Create wait for gradle sync. If you want to preview the interface, search for the compose entry function in setting->Plugins
is main() in Main.kt, and the style is also Material, full of Google flavor.
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);
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
The dependent lib and runtime are in the installation directory, execute the exe.
Click the button, there will be the unique water ripple effect on android
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.
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
https://github.com/JetBrains/compose-jb
Jetpack Compose for Desktop: Milestone 1 released
10 questions to help you understand Compose cross-platform
Compose Multiplatform for PC, Web and Android enters Alpha
This article quickly takes you to understand the status quo of KMM, Compose and Flutter
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