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
380
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
130
Building Shared UIs across Platforms with Compose
heyitsmohit
1
530
Building Multiplatform Apps with Compose
heyitsmohit
2
400
Building StateFlows with Jetpack Compose
heyitsmohit
6
1.7k
Building Android Testing Infrastructure
heyitsmohit
1
370
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
660
Using Square Workflow for Android & iOS
heyitsmohit
1
360
Building Android Infrastructure Teams at Scale
heyitsmohit
3
270
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
490
Other Decks in Programming
See All in Programming
C#/.NETのこれまでのふりかえり
tomokusaba
1
160
僕がつくった48個のWebサービス達
yusukebe
18
17k
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
150
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
270
CSC509 Lecture 09
javiergs
PRO
0
100
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
100
色々なIaCツールを実際に触って比較してみる
iriikeita
0
270
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.2k
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
ECSのサービス間通信 4つの方法を比較する 〜Canary,Blue/Greenも添えて〜
tkikuc
11
2.3k
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
RailsConf 2023
tenderlove
29
880
Building Your Own Lightsaber
phodgson
102
6k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Facilitating Awesome Meetings
lara
49
6k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Faster Mobile Websites
deanohume
304
30k
Code Review Best Practice
trishagee
64
17k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.2k
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