Thuy Phan - Android Engineer @LINE
@yoyocoder
KMM - Share code between
Native Android and Native iOS
About speaker
1
Slide 2
Slide 2 text
1. You would like to create a new mobile app for Android and iOS with native look and
feel but you don’t want to write the same code twice.
2. You have existing app for Android and iOS, you want to share code between 2
platforms at minimal cost.
Problem
2
Slide 3
Slide 3 text
3
02 What is KMM?
01 Approaches for a mobile app
03 What to share?
04 When to use KMM
05 Conclusion
Slide 4
Slide 4 text
4
Do you need a mobile app as
cheaply as possible?
Are you okay with having
limited performance if it
reduces time-to-market
Are you ready to have two dev
teams working on separate
codebases for each platform
Then, you need to
choose from these
options
Are you building an
ecommerce app?
Are hardware functionalities
like camera or GPS a must?
Hybrid app Cross-platform
app
Progressive web
app
Native app
Yes
No No
No
No Yes
Yes
Yes
No
Yes
Slide 5
Slide 5 text
5
02 What is KMM?
01 Approaches for a mobile app
03 What to share?
04 When to use KMM
05 Conclusion
Slide 6
Slide 6 text
What is KMM
6
Kotlin Multiplatforms
Mobile Features
+
Version >= 4.2
Kotlin Multiplatform Mobile plugin
Tooling
8
Version >= 11.3
Slide 9
Slide 9 text
KMM Plugin
9
Slide 10
Slide 10 text
10
Slide 11
Slide 11 text
Switch IDEs?
11
Slide 12
Slide 12 text
12
An Kotlin module that builds
into the Android application.
An Xcode project that builds
into the iOS application.
The code that works
on both platforms,
including the expect
declarations.
Android-specific parts,
including actual
implementations.
iOS-specific parts, including
actual implementations.
Slide 13
Slide 13 text
13
expect class Platform() {
val platform: String
}
actual class Platform actual constructor() {
actual val platform: String =
"Android ${android.os.Build.VERSION.SDK_INT}"
}
import platform.UIKit.UIDevice
actual class Platform actual constructor() {
actual val platform: String =
UIDevice.currentDevice.systemName() +
" " +
UIDevice.currentDevice.systemVersion
}
shared/androidMain shared/iosMain
shared/commonMain
Slide 14
Slide 14 text
14
02 What is KMM?
01 Approaches for a mobile app
03 What to share?
04 When to use KMM
05 Conclusion
Slide 15
Slide 15 text
Business / Domain
(Entities, Use Cases, Interacters)
Data / Core
(Repositories, HTTP Clients, Cache)
Architecture
15
Android
iOS
UI
(Views)
Presentation
(Presenters, View Models, Controllers)
Slide 16
Slide 16 text
Business logic and core
Shared code
Android specific APIs
CameraX
MLKit
Room
Biometric
Play Services
Lots of community and libraries
16
View View
Native code
iOS specific APIs
AVFoundation
CoreML
Local Authentication
Accounts
Cloudkit
Lots of community and libraries
1. You would like to create a new mobile app for Android and iOS with native look and
feel but you don’t want to write the same code twice.
2. You have existing app for Android and iOS, you want to share code between 2
platforms at minimal cost.
Problem
19
Slide 20
Slide 20 text
20
Write shared code in KMM Module Take code written in Kotlin for Android
Refactor it to be compatible with iOS
Implement Native UI on Android/iOS Project
New Project Existing Project
Connect .framework to iOS project and .jar to Android Project
Take a new feature
Slide 21
Slide 21 text
// sample code in shared/commonMain
class Greeting {
fun greeting(): String {
return "Hello, ${Platform().platform}!"
}
}
Unit testing
21
Slide 22
Slide 22 text
22
expect class Platform() {
val platform: String
}
actual class Platform actual constructor() {
actual val platform: String =
"Android ${android.os.Build.VERSION.SDK_INT}"
}
import platform.UIKit.UIDevice
actual class Platform actual constructor() {
actual val platform: String =
UIDevice.currentDevice.systemName() +
" " +
UIDevice.currentDevice.systemVersion
}
shared/androidMain shared/iosMain
shared/commonMain
Slide 23
Slide 23 text
// in shared/commonTest
class CommonGreetingTest {
@Test
fun testExample() {
assertTrue(
Greeting().greeting().contains("Hello"),
"Check 'Hello' is mentioned"
)
}
}
Unit testing
23
Slide 24
Slide 24 text
// in shared/androidTest
class AndroidGreetingTest {
@Test
fun testExample() {
assertTrue(
"Check Android is mentioned",
Greeting().greeting().contains("Android")
)
}
}
Unit testing
24
Slide 25
Slide 25 text
// in shared/iosTest
class IosGreetingTest {
@Test
fun testExample() {
assertTrue(
Greeting().greeting().contains("iOS"),
"Check iOS is mentioned"
)
}
}
Unit testing
25
Slide 26
Slide 26 text
No different from the usual process described in the Android developer
documentation from Google and the iOS developer documentation from Apple.
Test & Deploy
26
Slide 27
Slide 27 text
27
02 What is KMM?
01 Approaches for a mobile app
03 What to share?
04 When to use KMM
05 Conclusion
Slide 28
Slide 28 text
● Incrementally share code between platforms but the native UI.
● Your project isn't going to be released very soon.
● You don't care about KMM being in alpha status right now.
● Your app is complex, tied strongly to multi-thread or has background
processing.
When to use KMM
28
Slide 29
Slide 29 text
Who uses KMM
29
Slide 30
Slide 30 text
30
02 What is KMM?
01 Approaches for a mobile app
03 What to share?
04 When to use KMM
05 Conclusion
Slide 31
Slide 31 text
● KMM is still Alpha.
● 100% native look and feel on every new OS version.
● Single codebase for the business logic.
● Get all the cross-platform benefits in your existing project.
● Use iOS and Android features without any overhead.
● Use Kotlin for cross-platform code.
Conclusion
31