Slide 1

Slide 1 text

November 02, 2022 Isolated Development Ralf Wondratschek @vRallev

Slide 2

Slide 2 text

Challenges in 2018

Slide 3

Slide 3 text

Challenges in 2018: lines of code growth

Slide 4

Slide 4 text

Challenges in 2018: lines of code growth

Slide 5

Slide 5 text

Challenges in 2018: build times are growing

Slide 6

Slide 6 text

:hairball :app2 :app1 :lib1 :lib2 Challenges in 2018: module structure

Slide 7

Slide 7 text

:apps :feature :common :api Challenges in 2018: module structure

Slide 8

Slide 8 text

:hairball :app2 :app1 :lib1 :lib2 Challenges in 2018: multiple apps with different flavors

Slide 9

Slide 9 text

:hairball :app2 :app1 :lib1 :lib2 Challenges in 2018: multiple apps with different flavors

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

https://bit.ly/3D8SsUl

Slide 14

Slide 14 text

Dependency inversion

Slide 15

Slide 15 text

class Feature(s: LoginStrategy) interface LoginStrategy class SmsLoginStrategy() : LoginStrategy Dependency inversion

Slide 16

Slide 16 text

class SmsLoginStrategy @Inject constructor( private val helper: Helper, private val helper: Helper1, private val helper: Helper2, private val helper: Helper3, private val helper: Helper4, private val helper: Helper5, private val helper: Helper6, private val helper: Helper7, private val helper: Helper8 ) : LoginStrategy Dependency inversion

Slide 17

Slide 17 text

dependencies { api project(':helper1') api project(':helper2') api project(':helper3') api project(':helper4') api project(':helper5') api project(':helper6') api project(':helper7') api project(':helper8') } Dependency inversion

Slide 18

Slide 18 text

dependencies { api project(':login-strategy') } Dependency inversion

Slide 19

Slide 19 text

Module Structure :public :impl :impl-wiring

Slide 20

Slide 20 text

:login-strategy Module Structure :public :impl :impl-wiring

Slide 21

Slide 21 text

Module Structure :login-strategy:public :login-strategy:impl :login-strategy:impl-wiring

Slide 22

Slide 22 text

Module Structure

Slide 23

Slide 23 text

:login-strategy Module Structure :public :impl :impl-wiring :feature :public :impl :impl-wiring X

Slide 24

Slide 24 text

Development Apps :public :impl :impl-wiring

Slide 25

Slide 25 text

Development Apps :public :impl :impl-wiring :demo

Slide 26

Slide 26 text

Development Apps

Slide 27

Slide 27 text

Development Apps ● Install a feature with an isolated application on the device ○ Launch the feature directly ○ Shorter build times ● Easy way to experiment and merge code

Slide 28

Slide 28 text

:login-screen Development Apps :public :impl :impl-wiring :demo

Slide 29

Slide 29 text

:login-screen Development Apps :public :impl :impl-wiring :demo :development-app :public :impl :impl-wiring

Slide 30

Slide 30 text

:login-screen Development Apps :public :impl :impl-wiring :demo :development-app :public :impl :impl-wiring :ui-engine :public :impl

Slide 31

Slide 31 text

:login-screen Development Apps :public :impl :impl-wiring :demo

Slide 32

Slide 32 text

:login-screen Development Apps :public :impl :impl-wiring :demo :account-screen :public :impl

Slide 33

Slide 33 text

https://square.github.io/workflow/

Slide 34

Slide 34 text

:account-screen:public interface AccountScreenWorkflow : Workflow>

Slide 35

Slide 35 text

:account-screen:impl class RealAccountScreenWorkflow @Inject constructor( … ) : AccountScreenWorkflow

Slide 36

Slide 36 text

:login-screen:public interface LoginScreenWorkflow : Workflow>

Slide 37

Slide 37 text

:login-screen:impl class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow

Slide 38

Slide 38 text

:login-screen Development Apps :public :impl :impl-wiring :demo :account-screen :public :impl

Slide 39

Slide 39 text

:login-screen Development Apps :public :impl :impl-wiring :demo :account-screen :public :impl

Slide 40

Slide 40 text

:login-screen Development Apps :public :impl :impl-wiring :demo :account-screen :public :fake

Slide 41

Slide 41 text

:account-screen:fake class FakeAccountScreenWorkflow @Inject constructor() : AccountScreenWorkflow { // ... }

Slide 42

Slide 42 text

Module Structure :public :impl :impl-wiring :demo

Slide 43

Slide 43 text

Module Structure :public :impl :impl-wiring :demo :fake

Slide 44

Slide 44 text

:login-screen:demo android { defaultConfig { applicationId "com.squareup.sample.login.screen.demo" } } dependencies { // … implementation project(':account-screen:fake') implementation project(':development-app:impl-wiring) }

Slide 45

Slide 45 text

:login-screen:demo @DevelopmentAppComponent class DemoLoginApp : DevelopmentApplication() @Module @ContributesTo(ActivityScope::class) object DemoLoginMainActivityModule { @Provides fun provideFeatureWorkflowProvider( workflow: LoginScreenWorkflow ): FeatureProvider = FeatureWorkflowProvider( workflow = workflow, props = Unit, ) }

Slide 46

Slide 46 text

:login-screen:demo @DevelopmentAppComponent class DemoLoginApp : DevelopmentApplication() @Module @ContributesTo(ActivityScope::class) object DemoLoginMainActivityModule { @Provides fun provideFeatureWorkflowProvider( workflow: LoginScreenWorkflow ): FeatureProvider = FeatureWorkflowProvider( workflow = workflow, props = Unit, ) }

Slide 47

Slide 47 text

:login-screen:demo @DevelopmentAppComponent class DemoLoginApp : DevelopmentApplication() @Module @ContributesTo(ActivityScope::class) object DemoLoginMainActivityModule { @Provides fun provideFeatureWorkflowProvider( workflow: LoginScreenWorkflow ): FeatureProvider = FeatureWorkflowProvider( workflow = workflow, props = Unit, ) }

Slide 48

Slide 48 text

● Part of the development apps → Faster builds → Tend to be very stable → More build cache hits → Automatic test shardening ● Integration tests live in the final applications → Test robots are shared across all apps UI Tests

Slide 49

Slide 49 text

Mechanisms

Slide 50

Slide 50 text

plugins { id 'com.android.library id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.kapt' id 'com.squareup.anvil' } dependencies { api project(':account-screen:public') ... } tasks.withType(KotlinCompile).configureEach { kotlinOptions { jvmTarget = JavaVersion.VERSION_11 } } Mechanisms: module structure through convention plugins

Slide 51

Slide 51 text

// :login-screen:impl plugins { id 'com.squareup.android.lib } // :login-screen:demo plugins { id 'com.squareup.demo-app } Mechanisms: module structure through convention plugins https://developer.squareup.com/blog/herding-elephants/

Slide 52

Slide 52 text

class NoImplDependencyModuleCheck : ModuleCheck { override fun runCheck(project: Project) { … fail( "No :impl dependency is allowed. Requested: " + "${implDependencies.joinToString { it.dependencyName }} in project: $project." ) } } Mechanisms: module structure enforced through Lint rules

Slide 53

Slide 53 text

Mechanisms: library generator

Slide 54

Slide 54 text

Mechanisms: library generator

Slide 55

Slide 55 text

Mechanisms: library generator

Slide 56

Slide 56 text

Mechanisms: design patterns & frameworks ● Workflow → Composition was key → https://square.github.io/workflow/ ● Dependency inversion → Enforced through the module structure ● Dependency Injection → Anvil: https://github.com/square/anvil/ → Dagger 2: https://dagger.dev/

Slide 57

Slide 57 text

class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 58

Slide 58 text

class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow @Module abstract class LoginScreenWorkflowModule { @Binds abstract fun bindLoginScreenWorkflow( workflow: RealLoginScreenWorkflow ) : LoginScreenWorkflow } Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 59

Slide 59 text

class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow @Module abstract class LoginScreenWorkflowModule { @Binds abstract fun bindLoginScreenWorkflow( workflow: RealLoginScreenWorkflow ) : LoginScreenWorkflow } @Component( modules = { LoginScreenWorkflowModule::class, } ) interface AppComponent Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 60

Slide 60 text

@ContributesBinding(ActivityScope::class) class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 61

Slide 61 text

@ContributesBinding(ActivityScope::class) class RealLoginScreenWorkflow @Inject constructor( private val accountScreenWorkflow: Provider ) : LoginScreenWorkflow Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 62

Slide 62 text

@DevelopmentAppComponent class DemoLoginApp : DevelopmentApplication() @Module @ContributesTo(ActivityScope::class) object DemoLoginMainActivityModule { @Provides fun provideFeatureWorkflowProvider( workflow: LoginScreenWorkflow ): FeatureProvider = FeatureWorkflowProvider( workflow = workflow, props = Unit, ) } Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 63

Slide 63 text

@DevelopmentAppComponent class DemoLoginApp : DevelopmentApplication() @Module @ContributesTo(ActivityScope::class) object DemoLoginMainActivityModule { @Provides fun provideFeatureWorkflowProvider( workflow: LoginScreenWorkflow ): FeatureProvider = FeatureWorkflowProvider( workflow = workflow, props = Unit, ) } Mechanisms: design patterns & frameworks: Anvil https://github.com/square/anvil/

Slide 64

Slide 64 text

https://bit.ly/3WbPHdm

Slide 65

Slide 65 text

Mechanisms: design patterns & frameworks: Anvil https://bit.ly/3WbPHdm Try to build :demo module Add missing dependencies to build.gradle file Sync :demo module in Android Studio Add Dagger modules to Dagger components

Slide 66

Slide 66 text

Mechanisms: design patterns & frameworks: Anvil https://bit.ly/3WbPHdm Try to build :demo module Add missing dependencies to build.gradle file Sync :demo module in Android Studio Add Dagger modules to Dagger components

Slide 67

Slide 67 text

Mechanisms: dashboards

Slide 68

Slide 68 text

Mechanisms: dashboards

Slide 69

Slide 69 text

Mechanisms: dashboards

Slide 70

Slide 70 text

Mechanisms: dashboards

Slide 71

Slide 71 text

Mechanisms: dashboards

Slide 72

Slide 72 text

Mechanisms: dashboards

Slide 73

Slide 73 text

Mechanisms: dashboards

Slide 74

Slide 74 text

Mechanisms: dashboards

Slide 75

Slide 75 text

Mechanisms: benchmarks https://developer.squareup.com/blog/measure-measure-measure/

Slide 76

Slide 76 text

Mechanisms: benchmarks https://developer.squareup.com/blog/measure-measure-measure/

Slide 77

Slide 77 text

Mechanisms: benchmarks https://developer.squareup.com/blog/measure-measure-measure/

Slide 78

Slide 78 text

Mechanisms: migrations ● We were a small team and relied on the help of feature teams to contribute to common goals → Legacy module migration → Adopt Anvil → Extract UI tests into :demo apps https://www.droidcon.com/2022/09/29/migration-without-migraines-automatic-migration-at-scale/

Slide 79

Slide 79 text

Challenges

Slide 80

Slide 80 text

Challenges: module count

Slide 81

Slide 81 text

Solution: ● Convention plugins ● Benchmarks ● Anvil ● Configuration caching ● Partial IDE sync → https://github.com/dropbox/focus Challenges: module count

Slide 82

Slide 82 text

Legacy modules break our dependency rules Solution: ● Finish migration Challenges: legacy modules

Slide 83

Slide 83 text

Anti-pattern Challenges: granular modules

Slide 84

Slide 84 text

Challenges: granular modules :login-screen

Slide 85

Slide 85 text

Challenges: granular modules :login-screen :feature-a

Slide 86

Slide 86 text

Challenges: granular modules :login-screen :feature-a :feature-b

Slide 87

Slide 87 text

Challenges: granular modules :login-screen :feature-a :feature-b :login-strategy

Slide 88

Slide 88 text

Challenges: granular modules :login-screen :public :impl :impl-wiring

Slide 89

Slide 89 text

Challenges: granular modules :login-screen :public :impl-a :impl-a-wiring :impl-b :impl-b-wiring

Slide 90

Slide 90 text

Challenges: granular modules :login-screen :public :impl-a :impl-a-wiring :impl-b :impl-c :impl-b-wiring :impl-c-wiring

Slide 91

Slide 91 text

Challenges: development app shell

Slide 92

Slide 92 text

Challenges: extract UI tests

Slide 93

Slide 93 text

November 02, 2022 Isolated Development Ralf Wondratschek @vRallev