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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Mohit S
April 25, 2023
Programming
620
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
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
180
Lessons from Spec-Driven Development
simas
PRO
0
230
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
620
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.5k
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
260
AIで効率化できた業務・日常
ochtum
0
150
Vite+ Unified Toolchain for the Web
naokihaba
0
380
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
100
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
220
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
280
スマートグラスで並列バイブコーディング
hyshu
0
270
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.5k
Featured
See All Featured
Technical Leadership for Architectural Decision Making
baasie
3
430
Leo the Paperboy
mayatellez
7
1.9k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
320
30 Presentation Tips
portentint
PRO
1
330
We Are The Robots
honzajavorek
0
260
The Limits of Empathy - UXLibs8
cassininazir
1
370
Scaling GitHub
holman
464
140k
The Curious Case for Waylosing
cassininazir
1
410
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
200
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
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]