Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
KMPからPlarformコードを使いたい!
Search
NItakan
January 28, 2025
Programming
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
KMPからPlarformコードを使いたい!
NItakan
January 28, 2025
Other Decks in Programming
See All in Programming
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
4
3.5k
残高管理から台帳サービスへの進化
artoy
1
200
golang-bob.pdf
iwatsukayura
0
130
Domain-Driven Transformation
hschwentner
2
2.3k
作って学ぶSpring BootのAuto Configuration / JJUG Night Seminar 2026-10-19
rshindo
0
150
Next.js: The final boss of Web Performance
guaca
0
260
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
Workers Cache を知る
syumai
0
360
ジョジョの世界をXRで再現する
mofurune
0
160
AIに “答え“を書かせるのを やめてみた
___akari11
0
120
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
WENDY [Excerpt]
tessaabrams
14
40k
Making the Leap to Tech Lead
cromwellryan
135
10k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
520
A Tale of Four Properties
chriscoyier
163
24k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Building an army of robots
kneath
307
47k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
310
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
540
Transcript
©2025 Kyash Inc. ©2025 Kyash Inc. KMPか Platformコードを使いたい! 関西モバイルアプリ研究会A #7
2025/1/28 nitakan(KATO Masanori)
©2025 Kyash Inc. 自己紹介 object Nitakan { val id =
"nitakan" val name = "KATO Masanori" val company = "Kyash Inc." val xAccount = "@nitakanworld" val githubAccount = "@nitakan" }
©2025 Kyash Inc. はじめに 質問: KMP(Kotlin Multiplatform) を使っていますか
©2025 Kyash Inc. はじめに 🙆 KMPか Platformで実装したコードを利用す 方法 今日話したいこと
©2025 Kyash Inc. はじめに 🙆 KMPか Platformで実装したコードを利用す 方法 今日話したいこと 既存コードも活用してKMPをやってこうぜ!!!
©2025 Kyash Inc. はじめに 🙅 KMPの説明・導入 🙅 DI(Koin/swift-dependencies)について 🙅 KMP/Platformのアーキテクチャ
🙅 エラーハンドリング 🙅 Swift UI / Composeの実装 今日話さないこと
©2025 Kyash Inc. “ ” KMPか Android/iOSの既存コードを呼び出したい!
©2025 Kyash Inc. KMP→Platformコードの呼び出し • Android/iOSで処理を変えたい • KMP用のライブラリが提供さ ていない(Platform用のライ ブラリを使いたい)
• 既存のコードを活用したい • etc… たとえば
©2025 Kyash Inc. KMP→Platformコードの呼び出し • expect/actual ◦ KMPでexpect(interfaceみたいなもの)を定義し、各 Platform向けのactualを実装をす •
インターフェース + 依存性注入(DI) ◦ KMPでインターフェースを定義し、各Platform上で実装 す どんな 手段があ のか
©2025 Kyash Inc. KMP→Platformコードの呼び出し expect/actual
©2025 Kyash Inc. KMP→Platformコードの呼び出し インターフェース + 依存性注入(DI)
©2025 Kyash Inc. • expect/actual • インターフェース + 依存性注入 KMP→Platformコードの呼び出し
こっちの話をします
©2025 Kyash Inc. KMP→Platformコードの呼び出し interface PlatformRepository { fun getDataSync(): PlatformData
// ← 通常の関数 suspend fun getDataAsync(): PlatformData // ← suspend関数 } interface PlatformRepositoryFactory { fun createClientRepository(): PlatformRepository } Kotlin Native側でRepositoryインターフェースを実装
©2025 Kyash Inc. KMP→Platformコードの呼び出し class DisplayDataViewModel( private val kmpRepository: KmpRepository,
private val platformRepository: PlatformRepository, ) : ViewModel() { private val _displayData = MutableStateFlow<DisplayData>(DisplayData.loading()) @NativeCoroutinesState val displayData: StateFlow<DisplayData> = _displayData.asStateFlow() init { viewModelScope.launch { loadData() } } suspend fun loadData() { _displayData.emit(DisplayData.Loading) delay(1000) val platformSyncData = platformRepository.getDataSync() val kmp = kmpRepository.getKmpData() _displayData.emit(DisplayData.Loaded(platformSyncData, kmp)) val platformAyncData = platformRepository.getDataAsync() _displayData.emit(DisplayData.Loaded(platformAsyncData, kmp)) } } Kotlin Native側でViewModel実装
©2025 Kyash Inc. KMP→Platformコードの呼び出し class PlatformRepositoryImpl: PlatformRepository { override fun
getDataSync(): PlatformData { return PlatformData("[Android]The client generated sync data") } override suspend fun getDataAsync(): PlatformData { delay(1000L) return PlatformData("[Android]The client generated async data") } } Android側のRepository実装
©2025 Kyash Inc. KMP→Platformコードの呼び出し class MainApp : Application() { override
fun onCreate() { super.onCreate() initKoin( listOf( module { factory<PlatformRepository> { PlatformRepositoryImpl() } factory<PlatformRepositoryFactory> { PlatformRepositoryFactoryImpl(get()) } factory { DisplayDataViewModel(get(), get()) } } ) ) } } AndroidでViewModel生成時にRepositoryを渡す
©2025 Kyash Inc. KMP→Platformコードの呼び出し @Composable fun ListScreen() { val viewModel:
DisplayDataViewModel = koinViewModel() val objects by viewModel.displayData.collectAsStateWithLifecycle(DisplayData.loading()) val coroutine = rememberCoroutineScope() Content( item = objects, onLoad = { coroutine.launch { viewModel.loadData() } }, ) } AndroidでViewModelを利用す
©2025 Kyash Inc. KMP→Platformコードの呼び出し import Shared class PlatformRepositoryImpl {} extension
PlatformRepositoryImpl: PlatformRepository { func getDataSync() -> PlatformData { // ← 通常の関数の実装 PlatformData(value: "[iOS]The client generated sync data") } func getDataAsync() async throws -> PlatformData { // ← suspend関数の実装 _ = try await Task.sleep(nanoseconds: 1000_000_000) return PlatformData(value: "[iOS]The client generated async data") } } iOS側のRepository実装(Swift Concurrency)
©2025 Kyash Inc. KMP→Platformコードの呼び出し struct MainView: View { @Dependency(\.platformRepositoryFactory) var
repositoryFactory: PlatformRepositoryFactory var body: some View { DisplayView(platformRepository: repositoryFactory.createPlatformRepository()) } } struct DisplayView: View { var viewModel: DisplayDataViewModel init(platformRepository: PlatformRepository) { viewModel = DisplayDataViewModel( kmpRepository: KmpRepositoryImpl(), platformRepository: platformRepository ) } } iOSでViewModel生成時にRepositoryを渡す
©2025 Kyash Inc. KMP→Platformコードの呼び出し struct DisplayView: View { @State private
var displayData: DisplayData = DisplayData.Loading() var viewModel: DisplayDataViewModel init(platformRepository: PlatformRepository) { … } var body: some View { VStack(alignment: .center, spacing: 4) { switch (displayData) { case is DisplayData.Loading: Text("Loading...") … } } .task { do { for try await newValue in asyncSequence(for: viewModel.displayDataFlow) { self.displayData = newValue } } catch { print("Error collecting flow: \(error)") } } } } iOSでViewModelをUIで利用す
©2025 Kyash Inc. iOS KMP→Platform固有コードの呼び出し Android
©2025 Kyash Inc. おわ に Platform固有コードを呼び出す方法には大きく分けて2つのアプローチがあ • expect/actual を使った Kotlin言語上での切
分け • インターフェース+依存性注入(DI) を用いて、 Platform側の言語で実装 したコードをKMPへ 渡す方法 そ ぞ の使いどこ とメリット・デメリットを理解して、KMPライフを楽しみましょう 🍻🍻 まとめ
©2025 Kyash Inc. おわ に https://github.com/nitakan/kotlin-multiplatform-sandbox サンプルコード
©2025 Kyash Inc. ©2025 Kyash Inc. Thank you for listening🎉
©2025 Kyash Inc. • Expected and actual declarations | Kotlin
Multiplatform • Connect to platform-specific APIs | Kotlin Documentation • KMP-App-Template | GitHub Appendix