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
140
Building Shared UIs across Platforms with Compose
heyitsmohit
1
540
Building Multiplatform Apps with Compose
heyitsmohit
2
400
Building StateFlows with Jetpack Compose
heyitsmohit
6
1.7k
Building Android Testing Infrastructure
heyitsmohit
1
380
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
670
Using Square Workflow for Android & iOS
heyitsmohit
1
370
Building Android Infrastructure Teams at Scale
heyitsmohit
3
280
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
500
Other Decks in Programming
See All in Programming
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
110
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
受け取る人から提供する人になるということ
little_rubyist
0
230
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
C++でシェーダを書く
fadis
6
4.1k
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
For a Future-Friendly Web
brad_frost
175
9.4k
Code Review Best Practice
trishagee
64
17k
A Philosophy of Restraint
colly
203
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
We Have a Design System, Now What?
morganepeng
50
7.2k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Scaling GitHub
holman
458
140k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
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