Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Challenges of Building Kotlin Multiplatform Lib...
Search
Mohit S
March 25, 2022
Programming
1
390
Challenges of Building Kotlin Multiplatform Libraries
Mohit S
March 25, 2022
Tweet
Share
More Decks by Mohit S
See All by Mohit S
Guide to Improving Compose Performance
heyitsmohit
0
160
Building Shared UIs across Platforms with Compose
heyitsmohit
1
550
Building Multiplatform Apps with Compose
heyitsmohit
2
420
Building StateFlows with Jetpack Compose
heyitsmohit
6
1.7k
Building Android Testing Infrastructure
heyitsmohit
1
390
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
690
Using Square Workflow for Android & iOS
heyitsmohit
1
380
Building Android Infrastructure Teams at Scale
heyitsmohit
3
290
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
510
Other Decks in Programming
See All in Programming
MCP with Cloudflare Workers
yusukebe
2
220
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
730
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
190
CSC305 Lecture 26
javiergs
PRO
0
140
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
100
103 Early Hints
sugi_0000
1
230
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Designing for humans not robots
tammielis
250
25k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Scaling GitHub
holman
458
140k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Building an army of robots
kneath
302
44k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Transcript
Mohit Sarveiya Building Kotlin Multiplatform Libraries @heyitsmohit
Building Kotlin Multiplatform Libraries • Setting up KMM project •
CI Challenges • API Design Challenges
Why Kotlin Multiplatform? Android Kotlin/JVM iOS Swift/LLVM Web JS
Why Kotlin Multiplatform? API API API
Why Kotlin Multiplatform? Retrofit Ajax Alamofire
Why Kotlin Multiplatform? Room/SQL Delight Core Data
Why Kotlin Multiplatform? Business Logic Business Logic Business Logic
Why Kotlin Multiplatform? MVP/MVVM/MVI ReactJs VIPER
Why Kotlin Multiplatform? Shared code
Why Kotlin Multiplatform? API Business Logic Analytics Cache
Setting up KMM Project
Use Case Show platform name Hello Android
Hello iOS Use Case 9:41 Show platform name
Greeter Use Case
How to create Multiplatform app
Plugin https: // plugins.jetbrains.com/plugin/14936-kotlin-multiplatform-mobile
Project Setup Shared
Project Setup Shared Android App
Project Setup Shared Android App iOS App XCode Project
Project Setup Shared Android App iOS App
Project Setup Shared Android App iOS App Framework
Project Setup Shared Android App iOS App
Shared Module Structure
Shared Module Shared src commonMain
Shared Module Shared src commonMain androidMain
Shared Module Shared src commonMain androidMain iOSMain
Greeter Use Case
Common Shared Module Android iOS Platform name
Common expect class Platform() { val platform: String }
Common expect class Platform() { val platform: String }
Common expect class Platform() { val platform: String }
Common expect class Platform() { val platform: String } Common
Common Android Android iOS Platform name
Android actual class Platform actual constructor() { actual val platform:
String = ”Android” }
Android actual class Platform actual constructor() { actual val platform:
String = ”Android” }
Android actual class Platform actual constructor() { actual val platform:
String = ”Android” }
Android actual class Platform actual constructor() { actual val platform:
String = ”Android” } Common Android
Common iOS Android iOS Platform name
iOS actual class Platform actual constructor() { actual val platform:
String = “iOS” }
Common Shared Module Android iOS Platform name
Common class Greeter { fun greet(): String { return "Hello
${Platform().platform}!" } }
Common class Greeter { fun greet(): String { return "Hello
${Platform().platform}!" } }
Using Shared Module
Project Setup Shared Android App iOS App
Use Case Show platform name Hello Android
Android App class MainActivity : AppCompatActivity() { }
Android App class MainActivity : AppCompatActivity() { fun onCreate( ...
) { val greeting = Greeter().greet() } }
Android App class MainActivity : AppCompatActivity() { fun onCreate( ...
) { val greeting = Greeter().greet() textView.text = greeting } }
Android App class Greeter { ... } Common Source Build
Use Case Hello Android
Android App class MainActivity : AppCompatActivity() { fun onCreate( ...
) { val greeting = Greeter().greet() textView.text = greeting } }
Hello iOS Use Case 9:41 Show platform name
iOS App class Greeter { ... } Common Source Build
Framework
iOS App import SwiftUI import shared
iOS App import SwiftUI import shared func greet() -> String
{ return Greeting().greeting() }
iOS App import SwiftUI import shared struct ContentView: View {
var body: some View { } }
iOS App import SwiftUI import shared struct ContentView: View {
var body: some View { Text(greet()) } }
Hello iOS Use Case 9:41 Show platform name
Inside Objective-C Framework
iOS Framework class Greeter { ... } Common Source Build
Framework
iOS Framework import SwiftUI import shared func greet() -> String
{ return Greeting().greeting() }
iOS Framework Shared Base Shared Platform Shared Greeting
Shared Base __attribute__((swift_name("KotlinBase"))) @interface SharedBase : NSObject ... @end;
Shared Base __attribute__((swift_name("KotlinBase"))) @interface SharedBase : NSObject ... @end;
iOS actual class Platform actual constructor() { actual val platform:
String = “iOS” }
Mappings https: // kotlinlang.org/docs/native-objc-interop.html#mappings
Shared Platform __ attribute __ ((swift_name(“Platform"))) @interface SharedPlatform : SharedBase
... @end;
Shared Platform __ attribute __ ((swift_name(“Platform"))) @interface SharedPlatform : SharedBase
@property (readonly) NSString *platform @end;
Common class Greeter { fun greet(): String { return "Hello
${Platform().platform}!" } }
Common @interface SharedGreeting : SharedBase - (NSString *)greeting __ attribute
__ ((swift_name("greeting()"))); @end;
Android & iOS Shared Modules
Use Case Hello Android 29
Shared Module Shared src commonMain androidMain iOSMain
Android actual class Platform actual constructor() { actual val platform:
String = “Android ${android.os.Build.VERSION.SDK_INT}" }
Hello iOS 14.4 Use Case 9:41
Shared Module Shared src commonMain androidMain iOSMain
iOS import platform.UIKit.UIDevice actual class Platform actual constructor() { actual
val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion }
iOS import platform.UIKit.UIDevice actual class Platform actual constructor() { actual
val platform: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion }
Summary • How to build KMM project • Shared Module
Setup • Inside Shared Framework
CI Challenges
Swift Package Manager
Framework KMM Library
Swift Package Manager KMM Library
Multiplatform Swift Package • Gradle plugin • Generate XCFramework
Multiplatform Swift Package id("com.chromaticnoise.multiplatform-swiftpackage") version "2.0.3"
Multiplatform Swift Package multiplatformSwiftPackage { swiftToolsVersion(“5.5") targetPlatforms { iOS {
v("13") } } }
Multiplatform Swift Package ./gradlew createSwiftPackage
KMM Library XCEFramework
Projects/KMMProject Code ! Issues Pull Requests KMMLibrary.xcframework 1 commit 4c28f942
13 hours ago Package.swift KMMLibrary-1.0.zip README.md
Choose Package Repository Search or enter package repository URL Name
Last Updated Owner Projects(Github) KMMPackage Today 13:00 user
Add Package to Project Choose package and targets Kind Add
to Target KMMLibrary Library TestKMMLibrary Package Product
Steps • Create XCEFramework • Consume using Swift Package Manager
Build Automation
KMM Library bdae142 .. main 50157a .. d89f145 .. f0ddfb
..
bdae142 .. main 50157a .. d89f145 .. f0ddfb ..
KMM Library Jar XCEFramework
KMM Library Jar XCEFramework Artifactory
Artifactory
Build Automation • Setup CI to create artifcats • Use
artifacts from repository like Artifactory
API Design Challenges
Flows
Common fun getData(): Flow<Data>
Android val flow = getData() flow.collect { }
iOS getData( collector: T ## Kotlinx_coroutines_coreFlowCollector, completionHandler: (KotlinUnit?, Error?) ->
Void )
Common class CFlow internal constructor( val origin: Flow ) :
Flow by origin { fun watch(block: (T) -> Unit) { } }
Common class CFlow internal constructor( val origin: Flow ) :
Flow by origin { fun watch(block: (T) -> Unit) { onEach { block(it) } launchIn(CoroutineScope( ... )) } }
Common fun Flow<T>.wrap(): CFlow<T> = CFlow(this)
iOS flow .wrap() .watch { data -> }
Building Kotlin Multiplatform Libraries • Setting up KMM project •
CI Challenges • API Design Challenges
Thank You! www.codingwithmohit.com @heyitsmohit