Slide 1

Slide 1 text

Mohit Sarveiya Strategies for Migrating to Jetpack Compose @heyitsmohit

Slide 2

Slide 2 text

Strategies for Migrating to Jetpack Compose ● Challenges ● How to think about migration ● Strategies & Tools

Slide 3

Slide 3 text

Challenges

Slide 4

Slide 4 text

Code size Code 
 size Time

Slide 5

Slide 5 text

Greenfield Project ● Easier to use Compose ● Fewer roadblocks to use Compose

Slide 6

Slide 6 text

Code size Code 
 size Time

Slide 7

Slide 7 text

Roadmap 0% Compose 100% Compose

Slide 8

Slide 8 text

Roadmap 0% Compose 100% Compose Roadblock Roadblock

Slide 9

Slide 9 text

Roadmap ● Long road ● Obstacles along the way

Slide 10

Slide 10 text

Arch Fragmentation Codebase

Slide 11

Slide 11 text

Arch Fragmentation Codebase Legacy code

Slide 12

Slide 12 text

Arch Fragmentation Codebase Legacy code Feature A Feature B

Slide 13

Slide 13 text

Challenges ● Interoperability

Slide 14

Slide 14 text

Interop ● Compose to old view system

Slide 15

Slide 15 text

Interop Codebase Feature A Compose View Legacy code

Slide 16

Slide 16 text

Interop Codebase Legacy code Feature A Compose View

Slide 17

Slide 17 text

Interop ● Compose to old view system ● Use old views in Compose

Slide 18

Slide 18 text

Interop Codebase Legacy code Feature A Custom View

Slide 19

Slide 19 text

Interop Codebase Feature A Compose with 
 Custom View Legacy code

Slide 20

Slide 20 text

Challenges ● Interoperability ● Architecture

Slide 21

Slide 21 text

Architecture Codebase Legacy code

Slide 22

Slide 22 text

Arch Challenges ● Not using composition over inheritance

Slide 23

Slide 23 text

Legacy Code Base Fragment Too much logic

Slide 24

Slide 24 text

Legacy Code Base Fragment Too much logic Fragment Fragment Fragment …

Slide 25

Slide 25 text

Legacy Code Base Fragment Fragment Fragment Fragment … Refactor to Compose

Slide 26

Slide 26 text

Legacy Code Base View Holders Too much logic

Slide 27

Slide 27 text

Legacy Code Base View Holders Too much logic View Holder View Holder View Holder … Refactor to Compose

Slide 28

Slide 28 text

Arch Challenges ● Not using composition over inheritance ● Navigation

Slide 29

Slide 29 text

App Architecture API Domain Navigation UI

Slide 30

Slide 30 text

Navigation NavHost(navController, startDestination = "profile") { composable("profile") { Profile( /*...*/ ) } composable("friendslist") { FriendsList( /*...*/ ) } }

Slide 31

Slide 31 text

Legacy Navigation Main Activity Activity Activity

Slide 32

Slide 32 text

Navigation Main Activity Fragment A Fragment B Fragment C ...

Slide 33

Slide 33 text

Challenges ● Interoperability ● Architecture ● Testing

Slide 34

Slide 34 text

Testing Codebase 1% Code coverage 5% Code coverage

Slide 35

Slide 35 text

Challenges ● Interoperability ● Architecture ● Testing ● Tooling

Slide 36

Slide 36 text

Compose Upgrades Version Time alpha05 alpha06

Slide 37

Slide 37 text

Challenges ● Interoperability ● Architecture ● Testing ● Tooling

Slide 38

Slide 38 text

Collaboration Org CI Arch Design Team Feature Teams Teams

Slide 39

Slide 39 text

Strategies

Slide 40

Slide 40 text

Roadmap 0% Compose 100% Compose

Slide 41

Slide 41 text

Roadmap 0% Compose 100% Compose Milestone Milestone

Slide 42

Slide 42 text

Proposal ● Specify milestones over time ● Assess & Mitigate Risks ● Documentation

Slide 43

Slide 43 text

Roadmap 0% Compose 100% Compose Milestone

Slide 44

Slide 44 text

Complexity Codebase High Low Medium

Slide 45

Slide 45 text

Migration Phase 1 Complexity Migration time Low

Slide 46

Slide 46 text

Migration Phase 2 Complexity Migration time Medium

Slide 47

Slide 47 text

Migration Phase 1 ● Migrate Design Components

Slide 48

Slide 48 text

Example Feature A Feature B Feature C Common 
 Design Components

Slide 49

Slide 49 text

Example Common 
 Design Components App A App B App C

Slide 50

Slide 50 text

Collaboration Org CI Arch Design Team Feature Teams Teams

Slide 51

Slide 51 text

Migration Phase 1 ● Migrate design components to Compose

Slide 52

Slide 52 text

Interoperability

Slide 53

Slide 53 text

Interop 9:41

Slide 54

Slide 54 text

Interop 9:41 Convert to compose

Slide 55

Slide 55 text

Interop FrameLayout> 9:41

Slide 56

Slide 56 text

Interop FrameLayout> 9:41

Slide 57

Slide 57 text

Interop FrameLayout>

Slide 58

Slide 58 text

Compose View binding.composeView.apply { setContent { } }

Slide 59

Slide 59 text

Compose View binding.composeView.apply { setContent { MaterialTheme { DescriptionView() } } }

Slide 60

Slide 60 text

Challenges strings.xml dimen.xml theme.xml Compose Resuse

Slide 61

Slide 61 text

Compose View @Composable fun DescriptionView() { Text( text = stringResource(id = R.string.title) ) }

Slide 62

Slide 62 text

Compose View @Composable fun DescriptionView() { Text( modifier = Modifier.padding(dimensionResource(R.id.margin_small)) ) }

Slide 63

Slide 63 text

Challenges ● How do you reuse dimen and string resources? ● Interop Theming

Slide 64

Slide 64 text

Challenges strings.xml dimen.xml theme.xml Compose Resuse

Slide 65

Slide 65 text

Interop Theming implementation “com.google.android.material:compose-theme-adapter:1.1.1”

Slide 66

Slide 66 text

Interop Theming binding.composeView.apply { setContent { MaterialTheme { DescriptionView() } } }

Slide 67

Slide 67 text

Interop Theming binding.composeView.apply { setContent { MdcTheme { DescriptionView() } } }

Slide 68

Slide 68 text

Challenges ● How do you reuse dimen and string resources? ● Interop Theming ● View lifecycle

Slide 69

Slide 69 text

Compose View binding.composeView.apply { setContent { setViewCompositionStrategy( ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed ) } }

Slide 70

Slide 70 text

Phase 1 Migration Common 
 Design Components App A App B App C

Slide 71

Slide 71 text

Common Design Components ● View all compose components ● Document design system

Slide 72

Slide 72 text

airbnb/Showkase Code ! Issues Pull Requests Showkase Compatible with Compose 1.0.4 Showcase 1.0.0-beta12 Showkase is an annotation-processor based Android library that helps you organize, discover, search and visualize Jetpack Compose UI elements.

Slide 73

Slide 73 text

Showkase 9:41 App com.app General Logs Other Keyline overlay Slow down animations Network Activity Logs Lifecycle logs Show UX

Slide 74

Slide 74 text

Showkase 9:41 App com.app General Logs Other Keyline overlay Slow down animations Network Activity Logs Lifecycle logs Show UX 9:41 Design Components Components Colors Typography

Slide 75

Slide 75 text

Showkase Compose Design Spec Generate

Slide 76

Slide 76 text

Showkase @ShowkaseRoot class AppRootModule: ShowkaseRootModule

Slide 77

Slide 77 text

Showkase startActivity(Showkase.getBrowserIntent(this))

Slide 78

Slide 78 text

Showkase @Preview(name = “Custom component", group = "Custom group") @Composable fun PostView()

Slide 79

Slide 79 text

Showkase 9:41 Design Components Post view Title

Slide 80

Slide 80 text

Arch Fragmentation Codebase Legacy code 100% Compose 100% Compose

Slide 81

Slide 81 text

Interop @Composable fun PostView() { AndroidView(factory = { context -> })

Slide 82

Slide 82 text

Interop @Composable fun PostView() { AndroidView(factory = { context -> CustomView(context).apply { layoutParams = LinearLayout.LayoutParams( MATCH_PARENT, WRAP_CONTENT ) } })

Slide 83

Slide 83 text

Interop ● ComposeView ● AndroidView

Slide 84

Slide 84 text

Migration Phase 1 ● Migrate Design Components

Slide 85

Slide 85 text

Roadmap 0% Compose 100% Compose Milestone

Slide 86

Slide 86 text

Roadmap 0% Compose 100% Compose Milestone Milestone

Slide 87

Slide 87 text

Migration Phase 2 Complexity Migration time Medium

Slide 88

Slide 88 text

Testing Codebase 1% Code coverage 5% Code coverage

Slide 89

Slide 89 text

Migration Phase 2 ● Migrate medium complexity features ● Setup scaffolding to catch regressions

Slide 90

Slide 90 text

Testing

Slide 91

Slide 91 text

Testing Infra Compose Conversions Testing Infra

Slide 92

Slide 92 text

Goals ● Catch regressions earlier ● Increase covers with compose migration

Slide 93

Slide 93 text

Testing Infra ● Snapshot testing ● UI Testing Interop

Slide 94

Slide 94 text

cashapp/paparazzi Code ! Issues Pull Requests Paparazzi An Android library to render your application screens without a physical device or emulator.

Slide 95

Slide 95 text

Paparazzi Screenshot Unit Test

Slide 96

Slide 96 text

Paparazzi ● 1.0.0-Snapshot has compose support

Slide 97

Slide 97 text

Paparazzi @get:Rule val paparazzi = Paparazzi()

Slide 98

Slide 98 text

Paparazzi @get:Rule val paparazzi = Paparazzi() @Test fun testView() { paparazzi.snapshot { PostView(uiState) } }

Slide 99

Slide 99 text

Paparazzi 9:41 Post Title

Slide 100

Slide 100 text

Paparazzi Git (LFS) Snapshot

Slide 101

Slide 101 text

Testing Infra ● Snapshot testing ● UI Testing Interop

Slide 102

Slide 102 text

Interop FrameLayout> 9:41

Slide 103

Slide 103 text

Interop 9:41 Semantics Tree

Slide 104

Slide 104 text

Compose UI Testing @get:Rule val composeTestRule = createAndroidComposeRule()

Slide 105

Slide 105 text

Compose UI Testing @Test fun postViewTest() { composeTestRule.onNodeWithText(“Title").assertIsDisplayed() }

Slide 106

Slide 106 text

Interop @Composable fun PostView() { AndroidView(factory = { context -> CustomView(context) }) }

Slide 107

Slide 107 text

Interop @Test fun postViewTest() { composeTestRule.onNodeWithText("Submit").performClick() }

Slide 108

Slide 108 text

Interop @Test fun postViewTest() { composeTestRule.onNodeWithText(“Submit").performClick() Espresso.onView(withText(“Success")).check(matches(isDisplayed())) }

Slide 109

Slide 109 text

Migration Phase 2 ● Migrate medium complexity features ● Setup scaffolding to catch regressions

Slide 110

Slide 110 text

Migration Phases 0% Compose Milestones

Slide 111

Slide 111 text

Migration Phases ● Milestones depend on complexity & code size ● Low Complexity -> High Complexity

Slide 112

Slide 112 text

Migration Phases 0% Compose Milestones Testing Infra

Slide 113

Slide 113 text

Internationalization

Slide 114

Slide 114 text

Migration Phases 0% Compose Milestones

Slide 115

Slide 115 text

Languages Support strings.xml values-en values-de values-fr strings.xml strings.xml

Slide 116

Slide 116 text

Languages Support Add strings

Slide 117

Slide 117 text

Languages Support Translation service Add strings

Slide 118

Slide 118 text

Languages Support Translation service Provides translated strings Add strings

Slide 119

Slide 119 text

Languages Support ● Compose language interop

Slide 120

Slide 120 text

adrielcafe/lyricist Code ! Issues Pull Requests Lyricist The missing I18N and L10N multiplatform library for Jetpack Compose!

Slide 121

Slide 121 text

Languages Support Strings 
 (xml) Types KSP Composition 
 Local

Slide 122

Slide 122 text

Languages Support strings.xml values values-pt strings.xml

Slide 123

Slide 123 text

Languages Support Hello world string> Avocado item> string-array> %d zero item> plurals> resources>

Slide 124

Slide 124 text

Languages Support Olá mundo string> Abacate item> string-array> %d zero item> plurals> resources>

Slide 125

Slide 125 text

Languages Support ksp { arg("lyricist.xml.resourcesPath", 
 android.sourceSets.main.res.srcDirs.first().absolutePath) arg("lyricist.xml.moduleName", “xml") arg("lyricist.xml.defaultLanguageTag", "en") }

Slide 126

Slide 126 text

Languages Support data class Strings( val greeting: String, val array: List, val plurals: (quantity: Int) -> String ) object Locales { val En = "en" val Pt = "pt" }

Slide 127

Slide 127 text

Languages Support @Composable public fun ProvideStrings() { CompositionLocalProvider( provider provides lyricist.strings ) }

Slide 128

Slide 128 text

Languages Support @Preview @Composable fun PostViewPreview() { val xmlStrings = rememberXmlStrings() ProvideXmlStrings(xmlStrings) { Text(text = xmlStrings.greeting) } }

Slide 129

Slide 129 text

Languages Support @Preview @Composable fun PostViewPreview() { val xmlStrings = rememberXmlStrings() ProvideXmlStrings(xmlStrings) { Text(text = xmlStrings.greeting) } }

Slide 130

Slide 130 text

Languages Support @Preview @Composable fun PostViewPreview() { val xmlStrings = rememberXmlStrings() ProvideXmlStrings(xmlStrings) { Text(text = xmlStrings.greeting) } }

Slide 131

Slide 131 text

Languages Support @Preview @Composable fun PostViewPreview() 9:41 Post Hello World

Slide 132

Slide 132 text

Languages Support @Preview(locale = “pt”) @Composable fun PostViewPreview() 9:41 Post Olá mundo

Slide 133

Slide 133 text

Migration Phases 0% Compose Milestones

Slide 134

Slide 134 text

Tooling

Slide 135

Slide 135 text

Kotlin Upgrades This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.6.20 which is not known to be compatible.

Slide 136

Slide 136 text

Kotlin Upgrades composeOptions { 
 kotlinCompilerExtensionVersion compose_version 
 suppressKotlinVersionCompatibilityCheck true } Not Recommended

Slide 137

Slide 137 text

Kotlin Upgrades ● Wait for compose to be updated

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

Kotlin Upgrades ● Wait for compose to be updated ● Test out compose upgrades

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

Kotlin Upgrades ● Wait for compose to be updated ● Test out compose upgrades

Slide 142

Slide 142 text

Strategies for Migrating to Jetpack Compose ● Challenges ● How to think about migration ● Strategies & Tools

Slide 143

Slide 143 text

Thank You! www.codingwithmohit.com @heyitsmohit