Kotlin Multiplatform Guide
Introduction
Hello, I am writing this as I am working on a KMM project of my own. This will serve as guide on how to get started with a KMM and CMM project.
Key Points
- What is KMM
- Creating KMM Project
- How to start your Kotlin Multiplatform project.
- Package Structure
- How the project is structured
- What do the packages mean
- What is the difference between
iosApp
,androidApp
, andshared
- What is the difference between
shared
,iosMain
, andandroidMain
- What is CMM
- How to Setup CMM With Our KMM Project
Section 1: What is KMM
- KMM is Kotlin Multiplatform Mobile.
- Itβs meant to be a solution for cross platform development between Android and iOS. Serves as a means to share code between both platforms.
- In the case of the mobile scene even serve as a means to share UI code.
Section 2: Creating a KMM Project
Setup your environment
- Before creating your KMM project you want to make sure that you have your environment set up KMM development.
- Ensure you have Android Studio installed
- you can download the Jetbrains Toolbox to manage downloads and updates for Android Studio
- Install Xcode to run your iOS application on an simulated or real device.
- Install Java Development Kit(JDK)
- Run
java -version
in your terminal to check if you have Java installed. - You can also run it in your Android studio terminal
- Run
- Have the Kotlin Multiplatform Plugin installed in your Android Studio IDE
- Kotlin plugin
- Should come stock with Android Studio
Selecting KMM Project Configuration
- Now that we have the environment setup we can go 1 of 2 ways
- Creating the project via the Kotlin Multiplatform Wizard
- Creating the project via Android Studio
- I am doing it through Android Studio, doing it through the wizard is pretty straightforward.
- Click Kotlin Multiplatform App
2. I included test and selected Regular Framework
for the iOS framework distribution as dealing with cocoa pods and Ruby has come with a plethora of difficulties when dealing with it in the past.
3. Click Finish Now you have created a KMM project.
Section 3: Package Structure
KMM package structure is going to as follows
What is the difference between androidApp
, iosApp
, and shared
What is the difference between androidMain
, shared
, and iosMain
What is CMM
CMM or Compose Multiplatform mobile is a declarative framework for sharing UIs across multiple platforms. Based on Kotlin Multiplatform and Jetpack Compose.
How to Setup CMM With Our KMM Project
- You have a KMM project setup.
- Under
Gradle Scripts
- Go to
gradle.properties
- Go to
- Setup the versions for
kotlin
,agp
, andcompose
- Add them to the bottom of the file.
- While still in the
gradle.properties
we need to opt into experimental Compose Multiplatform APIβs
- Now go into
build.gradle.kts
project level- We need to specify the compose gradle plugin
- add
alias(libs.plugins.jetbrainsCompose)
andalias(libs.plugins.compose.compiler)
toplugins
ofbuild.gradle
androidApp levelbuild.gradle
shared level
- Once you have done that ensure that in your
build.gradle
shared level you addisStatic = true
- Otherwise the shared module will not be found in XCode
- Add the Compose Multiplatform dependencies in theΒ
sourceSets
Β block for the shared code:
9.Go to settings.gradle
- Add the Compose Multiplatform Maven path so that it finds
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
- Paste this plugins block in theΒ
pluginManagement
Β block:
- All together:
- Now that all the set up has been done we can head to
shared
βcommonMain
and create our shared Compose code!
Adding Compose View to Both Android and iOS with CMM
- I went and created a shared view in
shared
βcommonMain
- From here I went to
androidApp
βMainActivity
and referenced it in my UI. - To use it on iOS I went to
shared
βiosMain
and created akotlin
file and added the following
- You then go to
iosApp
βiosApp.xcodeproj
right click and clickopen in
βXcode
- It will open the project for you in the Xcode IDE
- Build and Run the code as is to sync it with our code
- Create a
.swift
file underiosApp
and call itComposeView
- Go to
ContentView
and update it