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
Building Multiplatform Apps with Compose
Search
Mohit S
April 25, 2023
Programming
630
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Building Multiplatform Apps with Compose
Mohit S
April 25, 2023
More Decks by Mohit S
See All by Mohit S
Guide to Improving Compose Performance
heyitsmohit
0
330
Building Shared UIs across Platforms with Compose
heyitsmohit
1
720
Building StateFlows with Jetpack Compose
heyitsmohit
6
2k
Building Android Testing Infrastructure
heyitsmohit
1
640
Migrating to Kotlin State & Shared Flows
heyitsmohit
1
880
Using Square Workflow for Android & iOS
heyitsmohit
1
530
Building Android Infrastructure Teams at Scale
heyitsmohit
3
430
Strategies for Migrating to Jetpack Compose
heyitsmohit
2
670
Challenges of Building Kotlin Multiplatform Libraries
heyitsmohit
1
520
Other Decks in Programming
See All in Programming
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
140
The NotImplementedError Problem in Ruby
koic
1
1k
Agentic UI
manfredsteyer
PRO
0
220
JavaDoc 再入門
nagise
1
440
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
180
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
200
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
310
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
150
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
250
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.5k
Featured
See All Featured
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
210
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.9k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
180
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
630
Everyday Curiosity
cassininazir
0
250
The Limits of Empathy - UXLibs8
cassininazir
1
380
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
250
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
450
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Transcript
Mohit Sarveiya Building Multiplatform Apps with Compose @
[email protected]
Building Multiplatform Apps with Compose • Setup Project
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop • Architecture & Navigation
Android Kotlin/JVM iOS Swift/LLVM Web JS Desktop Kotlin/JVM
Share
API Share
API Share Cache
API Share Cache Business Logic
API Share Cache Business Logic UI Components
https: / / github.com/JetBrains/compose-multiplatform compose-multiplatform
Compose Multiplatform • Android (via Jetpack Compose) • Desktop (Windows,
Mac OS, Linux) • Web (Experimental)
Compose Multiplatform • Android (via Jetpack Compose) • Desktop (Windows,
Mac OS, Linux) • Web (Experimental) • iOS (Alpha)
Compose Multiplatform
Compose Multiplatform
UI Structure
androidApp iOSApp desktopApp shared Structure
shared src commonMain androidMain iOSMain Shared Module
shared src commonMain androidMain iOSMain build.gradle.kts Shared Module
plugins { kotlin("multiplatform") } val commonMain by getting { dependencies
{ implementation(compose.ui) implementation(compose.foundation) implementation(compose.material) implementation(compose.runtime) } } Shared Module
plugins { kotlin("multiplatform") } val commonMain by getting { dependencies
{ implementation(compose.ui) implementation(compose.foundation) implementation(compose.material) implementation(compose.runtime) } } Shared Module
shared src commonMain androidMain iOSMain Shared Module
UI Structure App Root View Android iOS
UI Structure App Root View Android iOS
shared src commonMain androidMain iOSMain UI Structure
shared src commonMain androidMain iOSMain ImagesApp.commmon.kt UI Structure
fun ImagesAppCommon() { Scaffold( topBar = { TopAppBar( ... )
}, content = { ... } ) } UI Structure
fun ImagesAppCommon() { Scaffold( topBar = { TopAppBar( ... )
}, content = { ... } ) } UI Structure
shared src commonMain androidMain iOSMain ImagesApp.commmon.kt ImagesAppTheme.kt Shared Module
App Theme val LightColorPalette = lightColors( ... ) val DarkColorPalette
= darkColors( ... ) @Composable fun ImagesAppTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { MaterialTheme( colors = if (darkTheme) DarkColorPalette else LightColorPalette, content = content ) }
App Theme val LightColorPalette = lightColors( ... ) val DarkColorPalette
= darkColors( ... ) @Composable fun ImagesAppTheme( darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit ) { MaterialTheme( colors = if (darkTheme) DarkColorPalette else LightColorPalette, content = content ) }
shared src commonMain androidMain iOSMain ImagesApp.android.kt Shared Module
UI Structure @Composable fun MainAndroid() { ImagesAppTheme { ImagesAppCommon() }
}
androidApp iOSApp desktopApp shared UI Structure
UI Structure class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { MainAndroid() } } }
UI Structure class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState:
Bundle?) { super.onCreate(savedInstanceState) setContent { MainAndroid() } } }
shared src commonMain androidMain iOSMain ImagesApp.iOS.kt Shared Module
UI Structure fun MainiOS(): UIViewController ComposeUIViewController { ImagesAppCommon() }
UI Structure fun MainiOS(): UIViewController = ComposeUIViewController { ImagesAppCommon() }
androidApp iOSApp desktopApp shared UI Structure
UI Structure @main struct iOSApp: App { var body:
some Scene { WindowGroup { ContentView() } } }
UI Structure @main struct iOSApp: App { var body:
some Scene { WindowGroup { ContentView() } } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ComposeView: UIViewControllerRepresentable { func makeUIViewController(context: Context)
-> UIViewController { let controller = Main_iosKt.MainiOS() return controller } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } }
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
UI Structure struct ContentView: View { var body: some
View { ZStack { ComposeView() ... } } } UIWindowScene UIWindow ComposeWindow SkikkoUIView View Hierarchy
https: / / github.com/JetBrains/skiko Skiko
UI Structure App Root View Android iOS
UI Structure
Challenge: Image Loading
https: / / github.com/coil-kt/coil/issues/842 Coil-kt
shared src resource image1.jpeg image2.jpeg image3.jpeg Shared Module
val commonMain by getting { dependencies { implementation(compose.components.resources) } }
Shared Module
val commonMain by getting { dependencies { implementation(compose.components.resources) } }
Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes()
} Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
Except/Actual
shared src commonMain androidMain Shared Module iOSMain
expect fun ByteArray.toImageBitmap(): ImageBitmap Shared Module
shared src commonMain androidMain Shared Module iOSMain
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = toAndroidBitmap().asImageBitmap() fun ByteArray.toAndroidBitmap(): Bitmap {
return BitmapFactory.decodeByteArray(this, 0, size) } Shared Module
shared src commonMain androidMain Shared Module iOSMain
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
actual fun ByteArray.toImageBitmap(): ImageBitmap = Image.makeFromEncoded(this).toComposeImageBitmap() Shared Module
class ImageProvider { suspend fun getImageBitmap(picture: ImageData): ImageBitmap = resource(picture.url).readBytes().toImageBitmap()
} Shared Module
UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun ImagesList(images: List<ImageData>) { Column { LazyVerticalGrid( columns =
GridCells.Fixed(3) ) { items(images) { SplashImage( imageData = it ) } } } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
@Composable fun SplashImage(imageData: ImageData) { val imageProvider = LocalImageProvider.current var
imageBitmap by remember(imageData) { mutableStateOf<ImageBitmap?>(null) } LaunchedEffect(imageData) { imageBitmap = imageProvider.getImageBitmap(imageData) } imageBitmap ?. let { Image( bitmap = bitmap, ... ) } } UI Structure
UI Structure
UI Structure App Root View Android iOS Images List Images
Details Shared
Challenges: App Architecture, Navigation
Compose UI View Model UI State Events
UI Structure App Root View Android iOS Images List Images
Details Shared
UI Structure App Root View Android iOS Images List Images
Details Shared View Model
Shared Module shared src commonMain androidMain iOSMain
Shared Module shared src commonMain androidMain iOSMain ImagesListViewModel.kt
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture sealed class ImagesListUiState { object Loading: ImagesListUiState() data
class Success( val images: List<ImageData> ): ImagesListUiState() data class Error( val errorMessage: String ): ImagesListUiState() }
App Architecture class ImagesListViewModel { init { ... } val
state = MutableStateFlow<ImagesListUiState>(ImagesListUiState.Loading) val viewModelScope = CoroutineScope(Dispatchers.Main) } init } {
App Architecture class ImagesListViewModel { init { ... } }
init } { viewModelScope.launch(Dispatchers.Main) { try { val imagesList = imagesRepository.getImages() state.emit(uiState.Success(images = imagesList)) } catch (e: Exception) { state.emit(uiState.Error("Something went wrong")) } }
App Architecture @Composable fun ImagesAppCommon() { Scaffold( topBar = {
... }, content = { } ) }
App Architecture @Composable fun ImagesAppCommon() { Scaffold( topBar = {
... }, content = { } ) } val uiState by viewModel.state.collectAsState() ImagesListScreen(uiState)
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> is ImagesListUiState.Success -> is ImagesListUiState.Error -> } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> CircularProgressIndicator() is ImagesListUiState.Success -> is ImagesListUiState.Error -> } }
App Architecture @Composable fun ImagesListScreen(uiState: UIState) { when (uiState) {
ImagesListUiState.Loading -> CircularProgressIndicator() is ImagesListUiState.Success -> ImagesList(uiState.images) is ImagesListUiState.Error -> } }
Navigation App Root View List Details
App Architecture sealed class Screen { }
App Architecture sealed class Screen { object List : Screen()
}
App Architecture sealed class Screen { object List : Screen()
data class Details(val imageId: String) : Screen() }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.text, onBack = { screenState = Screen.List } ) } }
App Architecture @Composable fun ImagesAppCommon() { var screenState by remember
{ mutableStateOf<Screen>(Screen.List) } when (val screen = screenState) { is Screen.List -> List( onItemClick = { screenState = Screen.Details(imageId = it) } ) is Screen.Details -> Details( text = screen.imageId, onBack = { screenState = Screen.List } ) } }
App Architecture • Lifecycle Aware • Navigation
https: / / github.com/arkivanov/Decompose Decompose
Decompose • Lifecycle Aware Components • Back stack management
Decompose interface ListComponent { val uiState: Value<Model> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose interface ListComponent { val uiState: Value<UiState> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose interface ListComponent { val uiState: Value<UiState> fun onImageClicked(imageId:
String) data class UiState( val images: List<Image>, ) }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
Decompose class ImagesListComponent( componentContext: ComponentContext, val onImageSelected: (imageId: String) ->
Unit, ) : ListComponent { override val uiState: Value<ListComponent.UiState> = MutableValue(UiState.Loading) override fun onItemClicked(imageId: String) { onImageSelected(imageId) } }
@Composable fun ImagesList( component: ListComponent, images: List<ImageData>, onImageClicked: (Int) ->
Unit ) { val uiState by component.uiState.subscribeAsState() } Decompose
@Composable fun ImagesList( component: ListComponent, images: List<ImageData>, onImageClicked: (Int) ->
Unit ) { val uiState by component.uiState.subscribeAsState() } Decompose
Navigation App Root View List Details
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
interface RootComponent { val stack: Value<ChildStack <* , Child >>
sealed class Child { class ListChild(val component: ListComponent) : Child() class DetailsChild(val component: DetailsComponent) : Child() } } App Architecture
class DefaultRootComponent( ... ): RootComponent { @Parcelize sealed interface Config
: Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture val stack = childStack( source = navigation, initialConfiguration = Config.List, handleBackButton = true, childFactory = :: child, )
class DefaultRootComponent( ... ): RootComponent { val navigation =
StackNavigation<Config>() @Parcelize sealed interface Config : Parcelable { object List : Config data class Details(val item: String) : Config } } App Architecture
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
listComponent(): ListComponent = ImagesListComponent( onItemSelected = { imageId: String -> navigation.push(Config.Details(item = imageId)) }, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
listComponent(): ListComponent = ImagesListComponent( onItemSelected = { imageId: String -> navigation.push(Config.Details(item = imageId)) }, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
detailsComponent(): DetailsComponent = ImageDetailsComponent( image = config.image, onFinished = navigation :: pop, )
class DefaultRootComponent( ... ): RootComponent { } App Architecture fun
detailsComponent(): DetailsComponent = ImageDetailsComponent( image = config.image, onFinished = navigation :: pop, )
Navigation App Root View List Details
@Composable fun ImagesAppCommon(component: RootComponent) { Children( stack = component.stack )
{ when (val child = it.instance) { is ListChild -> ListContent(component = child.component) is DetailsChild -> DetailsContent(component = child.component) } } } App Architecture
@Composable fun ImagesAppCommon(component: RootComponent) { Children( stack = component.stack )
{ when (val child = it.instance) { is ListChild -> ListContent(component = child.component) is DetailsChild -> DetailsContent(component = child.component) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { RootContent(component = root, modifier = Modifier.fillMaxSize()) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { RootContent(component = root, modifier = Modifier.fillMaxSize()) } } } App Architecture
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState) val root = DefaultRootComponent( componentContext = defaultComponentContext(), ) setContent { MaterialTheme { Surface { ImageAppCommon(component = root) } } } App Architecture
class AppDelegate: NSObject, UIApplicationDelegate { let rootHolder: RootHolder = RootHolder()
} App Architecture
@main struct app_iosApp: App { var rootHolder: RootHolder {
appDelegate.rootHolder } var body: some Scene { WindowGroup { ComposeView(rootHolder.root) } } } App Architecture
Decompose • Lifecycle Aware Components • Back stack management
Building Multiplatform Apps with Compose • Setup Project • Share
Compose UI • SwiftUI & Compose Interop • Architecture & Navigation
Thank You! www.codingwithmohit.com @
[email protected]