• This repository has been archived on 20/Dec/2023
  • Stars
    star
    603
  • Rank 71,351 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 1 year ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compose Multiplatform Application project template

official project License

Compose Multiplatform application

Note
The iOS part of Compose Multiplatform is in Alpha. It may change incompatibly and require manual migration in the future. If you have any issues, please report them on GitHub.

You can use this template to start developing your own Compose Multiplatform application targeting desktop, Android, and iOS. Follow our tutorial below to get your first Compose Multiplatform app up and running. The result will be a Kotlin Multiplatform project that uses the Compose Multiplatform UI framework.

Set up the environment

Warning
You need a Mac with macOS to write and run iOS-specific code on simulated or real devices. This is an Apple requirement.

To work with this template, you need the following:

Check your environment

Before you start, use the KDoctor tool to ensure that your development environment is configured correctly:

  1. Install KDoctor with Homebrew:

    brew install kdoctor
    
  2. Run KDoctor in your terminal:

    kdoctor
    

    If everything is set up correctly, you'll see valid output:

    Environment diagnose (to see all details, use -v option):
    [โœ“] Operation System
    [โœ“] Java
    [โœ“] Android Studio
    [โœ“] Xcode
    [โœ“] Cocoapods
    
    Conclusion:
      โœ“ Your system is ready for Kotlin Multiplatform Mobile development!
    

Otherwise, KDoctor will highlight which parts of your setup still need to be configured and will suggest a way to fix them.

Examine the project structure

Open the project in Android Studio and switch the view from Android to Project to see all the files and targets belonging to the project:

Your Compose Multiplatform project includes 4 modules:

shared

This is a Kotlin module that contains the logic common for desktop, Android, and iOS applications, that is, the code you share between platforms.

This shared module is also where you'll write your Compose Multiplatform code. In shared/src/commonMain/kotlin/App.kt, you can find the shared root @Composable function for your app.

It uses Gradle as the build system. You can add dependencies and change settings in shared/build.gradle.kts. The shared module builds into a Java library, an Android library, and an iOS framework.

desktopApp

This is a Kotlin module that builds into a desktop application. It uses Gradle as the build system. The desktopApp module depends on and uses the shared module as a regular library.

androidApp

This is a Kotlin module that builds into an Android application. It uses Gradle as the build system. The androidApp module depends on and uses the shared module as a regular Android library.

iosApp

This is an Xcode project that builds into an iOS application. It depends on and uses the shared module as a CocoaPods dependency.

Run your application

On desktop

To run your desktop application in Android Studio, select desktopApp in the list of run configurations and click Run:


You can also run Gradle tasks in the terminal:

  • ./gradlew run to run application
  • ./gradlew package to store native distribution into build/compose/binaries

On Android

To run your application on an Android emulator:

  1. Ensure you have an Android virtual device available. Otherwise, create one.

  2. In the list of run configurations, select androidApp.

  3. Choose your virtual device and click Run:


Alternatively, use Gradle

To install an Android application on a real Android device or an emulator, run ./gradlew installDebug in the terminal.

On iOS

Running on a simulator

To run your application on an iOS simulator in Android Studio, modify the iosApp run configuration:

  1. In the list of run configurations, select Edit Configurations:

  2. Navigate to iOS Application | iosApp.

  3. In the Execution target list, select your target device. Click OK:

  4. The iosApp run configuration is now available. Click Run next to your virtual device:

Running on a real iOS device

You can run your Compose Multiplatform application on a real iOS device for free. To do so, you'll need the following:

  • The TEAM_ID associated with your Apple ID
  • The iOS device registered in Xcode

Note
Before you continue, we suggest creating a simple "Hello, world!" project in Xcode to ensure you can successfully run apps on your device. You can follow the instructions below or watch this Stanford CS193P lecture recording.

How to create and run a simple project in Xcode
  1. On the Xcode welcome screen, select Create a new project in Xcode.
  2. On the iOS tab, choose the App template. Click Next.
  3. Specify the product name and keep other settings default. Click Next.
  4. Select where to store the project on your computer and click Create. You'll see an app that displays "Hello, world!" on the device screen.
  5. At the top of your Xcode screen, click on the device name near the Run button.
  6. Plug your device into the computer. You'll see this device in the list of run options.
  7. Choose your device and click Run.
Finding your Team ID

In the terminal, run kdoctor --team-ids to find your Team ID. KDoctor will list all Team IDs currently configured on your system, for example:

3ABC246XYZ (Max Sample)
ZABCW6SXYZ (SampleTech Inc.)
Alternative way to find your Team ID

If KDoctor doesn't work for you, try this alternative method:

  1. In Android Studio, run the iosApp configuration with the selected real device. The build should fail.
  2. Go to Xcode and select Open a project or file.
  3. Navigate to the iosApp/iosApp.xcworkspace file of your project.
  4. In the left-hand menu, select iosApp.
  5. Navigate to Signing & Capabilities.
  6. In the Team list, select your team.

If you haven't set up your team yet, use the Add account option and follow the steps.

To run the application, set the TEAM_ID:

  1. In the template, navigate to the iosApp/Configuration/Config.xcconfig file.
  2. Set your TEAM_ID.
  3. Re-open the project in Android Studio. It should show the registered iOS device in the iosApp run configuration.

Make your first changes

You can now make some changes in the code and check that they are visible in both the iOS and Android applications at the same time:

  1. In Android Studio, navigate to the shared/src/commonMain/kotlin/App.kt file. This is the common entry point for your Compose Multiplatform app.

    Here, you see the code responsible for rendering the "Hello, World!" button and the animated Compose Multiplatform logo:

    @OptIn(ExperimentalResourceApi::class)
    @Composable
    internal fun App() {
        MaterialTheme {
            var greetingText by remember { mutableStateOf("Hello, World!") }
            var showImage by remember { mutableStateOf(false) }
            Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
                Button(onClick = {
                    greetingText = "Hello, ${getPlatformName()}"
                    showImage = !showImage
                }) {
                    Text(greetingText)
                }
                AnimatedVisibility(showImage) {
                    Image(
                        painterResource("compose-multiplatform.xml"),
                        null
                    )
                }
            }
        }
    }
  2. Update the shared code by adding a text field that will update the name displayed on the button:

    @OptIn(ExperimentalResourceApi::class)
    @Composable
    internal fun App() {
        MaterialTheme {
            var greetingText by remember { mutableStateOf("Hello, World!") }
            var showImage by remember { mutableStateOf(false) }
            Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
                Button(onClick = {
                    greetingText = "Hello, ${getPlatformName()}"
                    showImage = !showImage
                }) {
                    Text(greetingText)
                }
    +           TextField(greetingText, onValueChange = { greetingText = it })
                AnimatedVisibility(showImage) {
                    Image(
                        painterResource("compose-multiplatform.xml"),
                        null
                    )
                }
            }
        }
    }
  3. Re-run the desktopApp, androidApp, and iosApp configurations. You'll see this change reflected in all three apps:

How to configure the iOS application

To get a better understanding of this template's setup and learn how to configure the basic properties of your iOS app without Xcode, open the iosApp/Configuration/Config.xcconfig file in Android Studio. The configuration file contains:

To configure the APP_NAME option, open Config.xcconfig in any text editor before opening the project in Android Studio, and then set the desired name.

If you need to change this option after you open the project in Android Studio, do the following:

  1. Close the project in Android Studio.
  2. Run ./cleanup.sh in your terminal.
  3. Change the setting.
  4. Open the project in Android Studio again.

To configure advanced settings, use Xcode. After opening the project in Android Studio, open the iosApp/iosApp.xcworkspace file in Xcode and make changes there.

Next steps

We encourage you to explore Compose Multiplatform further and try out more projects:

More Repositories

1

kotlin

The Kotlin Programming Language.
Kotlin
45,725
star
2

intellij-community

IntelliJ IDEA Community Edition & IntelliJ Platform
16,452
star
3

compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
Kotlin
14,677
star
4

JetBrainsMono

JetBrains Mono โ€“ the free and open-source typeface for developers
Shell
10,126
star
5

ideavim

IdeaVim โ€“ A Vim engine for JetBrains IDEs
Kotlin
7,980
star
6

Exposed

Kotlin SQL Framework
Kotlin
7,869
star
7

kotlin-native

Kotlin/Native infrastructure
Kotlin
7,048
star
8

ring-ui

A collection of JetBrains Web UI components
TypeScript
3,555
star
9

kotlinconf-app

KotlinConf Schedule Application
Kotlin
2,830
star
10

intellij-platform-plugin-template

Template repository for creating plugins for IntelliJ Platform
Kotlin
2,791
star
11

skija

Java bindings for Skia
Java
2,605
star
12

create-react-kotlin-app

Create React apps using Kotlin with no build configuration
JavaScript
2,477
star
13

projector-docker

Run JetBrains IDEs remotely with Docker
Shell
2,209
star
14

intellij-plugins

Open-source plugins included in the distribution of IntelliJ IDEA Ultimate and other IDEs based on the IntelliJ Platform
Java
2,002
star
15

svg-sprite-loader

Webpack loader for creating SVG sprites.
JavaScript
1,998
star
16

skiko

Kotlin MPP bindings to Skia
C++
1,685
star
17

compose-multiplatform-ios-android-template

Compose Multiplatform iOS+Android Application project template
Kotlin
1,563
star
18

MPS

JetBrains Meta programming System
JetBrains MPS
1,500
star
19

lets-plot

Multiplatform plotting library based on the Grammar of Graphics
Kotlin
1,446
star
20

kotlin-web-site

The Kotlin programming language website
JavaScript
1,413
star
21

intellij-platform-gradle-plugin

Gradle plugin for building plugins for IntelliJ-based IDEs
Kotlin
1,377
star
22

phpstorm-stubs

PHP runtime & extensions header files for PhpStorm
PHP
1,297
star
23

kotlin-wrappers

Kotlin wrappers for popular JavaScript libraries
Kotlin
1,294
star
24

idea-gitignore

.ignore support plugin for IntelliJ IDEA
Kotlin
1,287
star
25

projector-server

Server-side library for running Swing applications remotely
Kotlin
1,230
star
26

resharper-unity

Unity support for both ReSharper and Rider
C#
1,199
star
27

intellij-sdk-docs

IntelliJ SDK Platform Documentation
Markdown
1,181
star
28

xodus

Transactional schema-less embedded database used by JetBrains YouTrack and JetBrains Hub.
Java
1,155
star
29

intellij-scala

Scala plugin for IntelliJ IDEA
Scala
1,137
star
30

kotless

Kotlin Serverless Framework
Kotlin
1,124
star
31

JetBrainsRuntime

Runtime environment based on OpenJDK for running IntelliJ Platform-based products on Windows, macOS, and Linux
Java
1,118
star
32

intellij-sdk-code-samples

Mirror of the IntelliJ SDK Docs Code Samples
Java
980
star
33

js-graphql-intellij-plugin

GraphQL language support for WebStorm, IntelliJ IDEA and other IDEs based on the IntelliJ Platform.
Java
879
star
34

android

Android Plugin for IntelliJ IDEA. This repository is a subset of https://git.jetbrains.org/?p=idea/android.git cut according to GitHub file size limitations.
Kotlin
863
star
35

projector-client

Common and client-related code for running Swing applications remotely
Kotlin
813
star
36

projector-installer

Install, configure and run JetBrains IDEs with Projector Server on Linux or in WSL
Python
810
star
37

Grammar-Kit

Grammar files support & parser/PSI generation for IntelliJ IDEA
Java
688
star
38

Arend

The Arend Proof Assistant
Java
676
star
39

amper

Amper - a build and project configuration tool with a focus on the user experience and the IDE support
Kotlin
621
star
40

markdown

Markdown parser written in kotlin
Kotlin
617
star
41

jediterm

Pure Java Terminal Emulator. Works with SSH and PTY.
Java
611
star
42

jewel

An implementation of the IntelliJ look and feels in Compose for Desktop
Kotlin
580
star
43

Nitra

Public Nitra repository
Nemerle
549
star
44

lincheck

Framework for testing concurrent data structures
Kotlin
520
star
45

intellij-micropython

Plugin for MicroPython devices in PyCharm and IntelliJ
Python
482
star
46

kotlin-playground

Self-contained component to embed in websites for running Kotlin code
JavaScript
422
star
47

colorSchemeTool

Python
396
star
48

compose-multiplatform-desktop-template

Compose Multiplatform Desktop Application project template
Kotlin
393
star
49

lets-plot-kotlin

Grammar of Graphics for Kotlin
Kotlin
389
star
50

Qodana

๐Ÿ“ Source repository of Qodana Help
388
star
51

rd

Reactive Distributed communication framework for .NET, Kotlin, C++. Inspired by Rider IDE.
C#
373
star
52

java-annotations

Annotations for JVM-based languages.
Java
362
star
53

phpstorm-attributes

PhpStorm specific attributes
PHP
357
star
54

Unity3dRider

Unity JetBrains Rider integration
348
star
55

godot-support

C#
339
star
56

pty4j

Pty for Java
Java
338
star
57

resharper-fsharp

F# support in JetBrains Rider
F#
320
star
58

phpstorm-workshop

Code for the PhpStorm workshop
PHP
287
star
59

awesome-pycharm

A curated list of resources for learning and using PyCharm, a Python IDE from JetBrains
271
star
60

web-types

JSON standard for documenting web component libraries for IDEs, documentation generators and other tools
TypeScript
270
star
61

meta-runner-power-pack

A set of Meta-runners for TeamCity
PowerShell
256
star
62

inspection-plugin

Gradle plugin to launch IDEA inspections
Kotlin
255
star
63

youtrack-mobile

A iOS and Android client for YouTrack
TypeScript
255
star
64

gradle-changelog-plugin

Plugin for parsing and managing the Changelog in a "keep a changelog" style.
Kotlin
252
star
65

ideolog

Interactive viewer for '.log' files.
Kotlin
250
star
66

qodana-action

โš™๏ธ Scan your Go, Java, Kotlin, PHP, Python, JavaScript, TypeScript, .NET projects at GitHub with Qodana. This repository contains Qodana for Azure, GitHub, CircleCI and Gradle
JavaScript
234
star
67

gradle-idea-ext-plugin

Plugin to store IJ settings in gradle script
Groovy
227
star
68

php-timeline

Notable events of PHP history
222
star
69

resharper-rider-samples

Simple interactive exercises to help learn ReSharper and Rider
C#
221
star
70

la-clojure

Clojure plugin for IntelliJ IDEA
Java
220
star
71

kotlin-compiler-server

Server for executing kotlin code
Kotlin
216
star
72

jdk8u_jdk

Java
210
star
73

jcef

A simple framework for embedding Chromium-based browsers into Java-based applications.
Java
206
star
74

pest-intellij

The official Pest Plugin for PhpStorm / IntelliJ IDEA
Kotlin
195
star
75

youtrack-workflows

YouTrack Custom Workflow Repository
JavaScript
194
star
76

psiviewer

PSI Viewer for IntelliJ IDEA plugin development
Java
175
star
77

svg-mixer

Node.js toolset for generating & transforming SVG images and sprites in modern way
JavaScript
173
star
78

compose-for-web-demos

Historical repository of early Compose for Web effort.
171
star
79

phpstorm-docker-images

Pre-configured Docker images used by PhpStorm team
Dockerfile
170
star
80

rider-efcore

Entity Framework Core UI plugin for JetBrains Rider
Kotlin
169
star
81

jetbrains_guide

JetBrains Guides where Developer Advocacy and the community share ideas.
CSS
168
star
82

kotlin-web-demo

Online mini-IDE for Kotlin
Kotlin
168
star
83

intellij-plugin-verifier

Compatibility verification tool for IntelliJ Platform plugins
Kotlin
165
star
84

intellij-samples

Code that demonstrates various IntelliJ IDEA features
Java
163
star
85

jdk8u_hotspot

C++
159
star
86

resharper-rider-plugin

https://www.jetbrains.com/help/resharper/sdk/
PowerShell
158
star
87

qodana-cli

๐Ÿ”ง JetBrains Qodanaโ€™s official command line tool
Go
154
star
88

teamcity-messages

Python Unit Test Reporting to TeamCity
Python
139
star
89

ruby-type-inference

Dynamic definitions and types provider for ruby static analysis
Kotlin
136
star
90

educational-plugin

Educational plugin to learn and teach programming languages such as Kotlin, Java, Python, JavaScript, and others right inside of JetBrains IntelliJ Platform based IDEs.
Kotlin
134
star
91

resharper-angularjs

ReSharper plugin for AngularJS support
JavaScript
134
star
92

clion-remote

134
star
93

golandtipsandtricks

This is an ever evolving repository for GoLand Tips&Tricks
Go
132
star
94

python-skeletons

The python-skeltons repo is deprecated: use PEP 484 and Typeshed instead
Python
132
star
95

clion-wsl

Shell
130
star
96

phpstorm-phpstan-plugin

PHPStan plugin for PhpStorm
Java
130
star
97

teamcity-docker-samples

TeamCity docker compose samples
Shell
128
star
98

phpstorm-psalm-plugin

Psalm plugin for PhpStorm
Java
126
star
99

jdk8u

Shell
123
star
100

YouTrackSharp

.NET Standard 2.0 Library to access YouTrack API.
C#
123
star