Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why we failed at modularizing our app

Marcos
July 03, 2019

Why we failed at modularizing our app

Modularization is the new trend, almost everybody in the Android ecosystem is refactoring their apps to use a modularized approach. We at Sky are no different, we had a big monolithic codebase supporting 4 apps in different countries that we started modularizing in September 2017. But we failed, big time.

This talk is an honest retrospective of everything that went wrong, the bad decisions that we made, the approach we initially took and how we, against all odds, eventually started re-building a maintainable, sustainable and extensible modularized codebase. In this talk you will learn from our mistakes and struggles, like defining what is a module and its responsibilities, how to integrate Dagger in a multi-module environment, set some rules and best practices and much more but, more importantly, you will learn what not to do when modularizing your codebase.

Marcos

July 03, 2019
Tweet

More Decks by Marcos

Other Decks in Programming

Transcript

  1. productFlavors { uk { ... apply from: 'uk.gradle' } international

    { ... apply from: 'international.gradle' } germany { ... apply from: 'germany.gradle' } italia { ... apply from: 'italy.gradle' } } @Orbycius
  2. productFlavors { uk { ... apply from: 'uk.gradle' } international

    { ... apply from: 'international.gradle' } germany { ... apply from: 'germany.gradle' } italia { ... apply from: 'italy.gradle' } } @Orbycius App
  3. productFlavors { uk { ... apply from: 'uk.gradle' } international

    { ... apply from: 'international.gradle' } germany { ... apply from: 'germany.gradle' } italia { ... apply from: 'italy.gradle' } } @Orbycius App src
  4. productFlavors { uk { ... apply from: 'uk.gradle' } international

    { ... apply from: 'international.gradle' } germany { ... apply from: 'germany.gradle' } italia { ... apply from: 'italy.gradle' } } @Orbycius App src main
  5. productFlavors { uk { ... apply from: 'uk.gradle' } international

    { ... apply from: 'international.gradle' } germany { ... apply from: 'germany.gradle' } italia { ... apply from: 'italy.gradle' } } @Orbycius App src main germany uk italia international
  6. @Orbycius Author: Marcos Holgado <…@sky.uk> Date: Bug fix for tab

    colours... again Tue Oct 17 09:36:44 2017 +0100
  7. @Orbycius Author: Marcos Holgado <…@sky.uk> Date: Bug fix for tab

    colours... again Author: Marcos Holgado <…@sky.uk> Date: Fix tab colours again :( Tue Oct 17 09:36:44 2017 +0100 Mon Oct 23 16:10:08 2017 +0100
  8. @Orbycius Author: Marcos Holgado <…@sky.uk> Date: Bug fix for tab

    colours... again Author: Marcos Holgado <…@sky.uk> Date: Fix tab colours again :( Tue Oct 17 09:36:44 2017 +0100 Mon Oct 23 16:10:08 2017 +0100
  9. Long build times Change one break many Painful to work

    with It doesn’t scale … @Orbycius
  10. @Orbycius @Provides public ForegroundManager provideForegroundManager(Context context) { useForegroundManager = new

    ForegroundManager((Application) context); if (!foregroundManager.compareAndSet(null, useForegroundManager)) { useForegroundManager = foregroundManager.get(); } } return useForegroundManager; } private final AtomicReference<ForegroundManager> foregroundManager; ForegroundManager useForegroundManager = foregroundManager.get(); if (useForegroundManager == null) { } @Singleton
  11. @Orbycius @Provides public ForegroundManager provideForegroundManager(Context context) { useForegroundManager = new

    ForegroundManager((Application) context); if (!foregroundManager.compareAndSet(null, useForegroundManager)) { useForegroundManager = foregroundManager.get(); } } return useForegroundManager; } private final AtomicReference<ForegroundManager> foregroundManager; ForegroundManager useForegroundManager = foregroundManager.get(); if (useForegroundManager == null) { } @Singleton
  12. @Orbycius @Provides public ForegroundManager provideForegroundManager(Context context) { useForegroundManager = new

    ForegroundManager((Application) context); if (!foregroundManager.compareAndSet(null, useForegroundManager)) { useForegroundManager = foregroundManager.get(); } } return useForegroundManager; } private final AtomicReference<ForegroundManager> foregroundManager; ForegroundManager useForegroundManager = foregroundManager.get(); if (useForegroundManager == null) { } @Singleton
  13. @Orbycius public class PlayerActivity extends AppCompatActivity { @Inject Utility utility;

    @Override protected void onCreate(Bundle savedInstanceState) { .getStreamingComponent() .addSubcomponent(new StreamingModule(this)) .inject(this); } [...] } } // Check that a sub-class hasn't just done DI for us! if (utility == null) { StreamingModuleMain.getStreamingModuleHelper()
  14. @Orbycius public class PlayerActivity extends AppCompatActivity { @Inject Utility utility;

    @Override protected void onCreate(Bundle savedInstanceState) { .getStreamingComponent() .addSubcomponent(new StreamingModule(this)) .inject(this); } [...] } } // Check that a sub-class hasn't just done DI for us! if (utility == null) { StreamingModuleMain.getStreamingModuleHelper()
  15. @Orbycius public class PlayerActivity extends AppCompatActivity { @Inject Utility utility;

    @Override protected void onCreate(Bundle savedInstanceState) { .getStreamingComponent() .addSubcomponent(new StreamingModule(this)) .inject(this); } [...] } } // Check that a sub-class hasn't just done DI for us! if (utility == null) { StreamingModuleMain.getStreamingModuleHelper()
  16. @Orbycius public interface ModuleMain { void initialise(); void terminate(); BaseNavObjectRegistrar

    getNavObjectRegistrar(); } public interface ModuleHelper { void setCoreComponent(CoreComponent coreComponent); CoreComponent getCoreComponent(); } ModuleHelper getModuleHelper();
  17. App @Orbycius Streaming @Component(modules = [AppModule::class]) interface AppComponent { fun

    inject(mainActivity: MainActivity) fun plus( ) } @Subcomponent(modules = [StreamingModule::class]) interface StreamingSubcomponent { fun inject(activity: PlayerActivity) } streamingComponent: StreamingSubcomponent
  18. App @Orbycius Streaming @Component(modules = [AppModule::class]) interface AppComponent { fun

    inject(mainActivity: MainActivity) fun plus( ) } @Subcomponent(modules = [StreamingModule::class]) interface StreamingSubcomponent { fun inject(activity: PlayerActivity) } streamingComponent: StreamingSubcomponent
  19. App @Orbycius Streaming @Component(modules = [AppModule::class]) interface AppComponent { fun

    inject(mainActivity: MainActivity) fun plus( ) } @Subcomponent(modules = [StreamingModule::class]) interface StreamingSubcomponent { fun inject(activity: PlayerActivity) } streamingComponent: StreamingSubcomponent
  20. App @Orbycius Streaming .plus(streamingSubcomponent) .inject(this) @Component(modules = [AppModule::class]) interface AppComponent

    { fun inject(mainActivity: MainActivity) fun plus( ) } streamingComponent: StreamingSubcomponent appComponent
  21. App @Orbycius Streaming .plus(streamingSubcomponent) .inject(this) @Component(modules = [AppModule::class]) interface AppComponent

    { fun inject(mainActivity: MainActivity) fun plus( ) } streamingComponent: StreamingSubcomponent appComponent
  22. App @Orbycius Streaming .plus(streamingSubcomponent) .inject(this) @Component(modules = [AppModule::class]) interface AppComponent

    { fun inject(mainActivity: MainActivity) fun plus( ) } streamingComponent: StreamingSubcomponent appComponent
  23. App @Orbycius Streaming Core interface CoreComponentProvider { CoreComponent provideCoreComponent(); }

    interface AppComponentProvider { AppComponent provideAppComponent(); } interface StreamingComponentProvider { StreamingComponent provideStComponent(); }
  24. App @Orbycius Streaming Core interface CoreComponentProvider { CoreComponent provideCoreComponent(); }

    interface AppComponentProvider { AppComponent provideAppComponent(); } interface StreamingComponentProvider { StreamingComponent provideStComponent(); } class SkySportsApplication extends Application implements CoreComponentProvider, AppComponentProvider { }
  25. @Orbycius interface AppComponentProvider { AppComponent provideAppComponent(); } class SkySportsApplication extends

    Application implements CoreComponentProvider, AppComponentProvider { private CoreComponent coreComponent; private AppComponent appComponent; @Override public CoreComponent provideCoreComponent() { if (coreComponent == null) { coreComponent = DaggerCoreComponent.builder() .commonModule(new CommonModule(this)) .build(); } return commonComponent; } }
  26. @Orbycius public class UKSportsApplication extends implements { private StreamingComponent streamingComponent;

    } SkySportsApplication StreamingComponentProvider .coreComponent(provideCoreComponent()) @Override public StreamingComponent provideStreamingComponent() { if (streamingComponent == null) { streamingComponent = DaggerStreamingComponent.builder() .streamingModule(new StreamingModule( .build(); } return streamingComponent; } provideAppComponent().getUser()
  27. @Orbycius public class UKSportsApplication extends implements { private StreamingComponent streamingComponent;

    } SkySportsApplication StreamingComponentProvider .coreComponent(provideCoreComponent()) @Override public StreamingComponent provideStreamingComponent() { if (streamingComponent == null) { streamingComponent = DaggerStreamingComponent.builder() .streamingModule(new StreamingModule( .build(); } return streamingComponent; } provideAppComponent().getUser()
  28. @Orbycius public class UKSportsApplication extends implements { private StreamingComponent streamingComponent;

    } SkySportsApplication StreamingComponentProvider .coreComponent(provideCoreComponent()) @Override public StreamingComponent provideStreamingComponent() { if (streamingComponent == null) { streamingComponent = DaggerStreamingComponent.builder() .streamingModule(new StreamingModule( .build(); } return streamingComponent; } provideAppComponent().getUser()
  29. @Orbycius public class UKSportsApplication extends implements { private StreamingComponent streamingComponent;

    } SkySportsApplication StreamingComponentProvider .coreComponent(provideCoreComponent()) @Override public StreamingComponent provideStreamingComponent() { if (streamingComponent == null) { streamingComponent = DaggerStreamingComponent.builder() .streamingModule(new StreamingModule( .build(); } return streamingComponent; } provideAppComponent().getUser()
  30. @Orbycius public class UKSportsApplication extends implements { private StreamingComponent streamingComponent;

    } SkySportsApplication StreamingComponentProvider .coreComponent(provideCoreComponent()) @Override public StreamingComponent provideStreamingComponent() { if (streamingComponent == null) { streamingComponent = DaggerStreamingComponent.builder() .streamingModule(new StreamingModule( .build(); } return streamingComponent; } provideAppComponent().getUser()
  31. @Orbycius public class PlayerActivity extends AppCompatActivity { @Inject Utility utility;

    @Inject User user; @Override protected void onCreate(Bundle savedInstanceState) { StreamingInjectHelper.provideStreamingComponent( getApplicationContext() ).inject(this); } }
  32. @Orbycius public class StreamingInjectHelper { public static StreamingComponent provideStreamingComponent(final Context

    context){ if (context instanceof StreamingComponentProvider) { return ((StreamingComponentProvider)context).provideStreamingComponent(); } else { throw new IllegalStateException("The context you have passed does not implement StreamingComponentProvider"); } } }
  33. @Orbycius @Provides @IntoMap @IntKey(TableRow.CRICKET_ROW) SportsListViewHolderFactory provideCricketViewHolder() { return new CricketStandingViewHolderFactory();

    } @Provides @IntoMap @IntKey(TableRow.RUGBY_ROW) SportsListViewHolderFactory provideRugbyViewHolder() { return new RugbyStandingViewHolderFactory(); } @Provides @IntoMap @IntKey(TableRow.F1_ROW) SportsListViewHolderFactory provideF1DriverViewHolder() { return new F1DriverStandingViewHolderFactory(); }
  34. @Orbycius @Provides Map<Integer, SportsListViewHolderFactory> provideSportsListViewHolderFactoryMap() { Map<Integer, SportsListViewHolderFactory> map =

    new HashMap<>(); map.put(TableRow.F1_ROW, new F1DriverStandingViewHolderFactory()); map.put(TableRow.CRICKET_ROW, new CricketStandingViewHolderFactory()); map.put(TableRow.RUGBY_ROW, new RugbyStandingViewHolderFactory()); return map; }
  35. @Orbycius public class Config implements Parcelable { /** * Config

    Sections */ private final Map<String, ConfigSection> sections; }
  36. @Orbycius public class Config implements Parcelable { /** * Config

    Sections */ private final Map<String, ConfigSection> sections; } public interface ConfigSection extends Parcelable { /** * Name of the section to use as a key * * @return Name of the section to use for lookups */ String getSectionName(); }
  37. @Orbycius public class ConfigDeserialiser implements JsonDeserializer<Config> { /** * Deserialiser

    for individual sections, keyed on the sections they support * * Note that this means each deserialiser may appear more than once! */ private final Map<String, ConfigSectionDeserialiser> sectionDeserialisers; }
  38. @Orbycius public class ConfigDeserialiser implements JsonDeserializer<Config> { /** * Deserialiser

    for individual sections, keyed on the sections they support * * Note that this means each deserialiser may appear more than once! */ private final Map<String, ConfigSectionDeserialiser> sectionDeserialisers; } public interface ConfigSectionDeserialiser<T extends ConfigSection> { Collection<String> supportedParentKeys(); String sectionKey(); @NonNull T deserialiseSection(String key, JsonElement sectionSource, @Nullable T existingSection); T getDefaultSection(); }
  39. @Orbycius public class ForcedUpgradeDeserialiser implements ConfigSectionDeserialiser<ForcedUpgrade> { public static final

    String LATEST_VERSION = "lver"; public static final String MESSAGE_TITLE = "title"; public static final String MESSAGE_BODY = "message"; public static final String FORCE_UPGRADE = "shouldForce"; }
  40. @Orbycius public class ForcedUpgradeDeserialiser implements ConfigSectionDeserialiser<ForcedUpgrade> { public static final

    String LATEST_VERSION = "lver"; public static final String MESSAGE_TITLE = "title"; public static final String MESSAGE_BODY = "message"; public static final String FORCE_UPGRADE = "shouldForce"; } public class ForcedUpgrade implements ConfigSection { public static final String FORCED_UPGRADE = "shouldForce"; /** * Latest version code */ private int latestVersion; }
  41. @Orbycius “Deleted a lot of classes around config because let's

    be honest, is just a json so why over complicating it?”
  42. @Orbycius “Deleted a lot of classes around config because let's

    be honest, is just a json so why over complicating it?”
  43. @Orbycius Feature 1 Feature 2 Feature 3 Feature 4 Feature

    5 Feature 6 Feature 7 Feature 8 Feature 9
  44. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> implementation

    project(path: ':core') implementation project(path: ':streaming') <string name="test">Core test</string>
  45. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> implementation

    project(path: ':core') implementation project(path: ':streaming') Core test <string name="test">Core test</string>
  46. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> <string

    name="test">Core test</string> implementation project(path: ':streaming') implementation project(path: ':core')
  47. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> My

    test <string name="test">Core test</string> implementation project(path: ':streaming') implementation project(path: ':core')
  48. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> <string

    name="test">Core test</string> implementation project(path: ':streaming') implementation project(path: ':core') <string name="test">App test</string>
  49. App @Orbycius Streaming Core <TextView android:text="@string/test"/> <string name="test">My test</string> App

    test <string name="test">Core test</string> implementation project(path: ':streaming') implementation project(path: ':core') <string name="test">App test</string>
  50. @Orbycius android { compileSdkVersion androidCompileSdkVersion defaultConfig { minSdkVersion androidMinSdkVersion targetSdkVersion

    androidTargetSdkVersion versionCode 1 versionName "1.0" } resourcePrefix 'my_prefix' }
  51. @Orbycius <resources> <string name="test">My other test</string> </resources> Resource named '`test`'

    does not start with the project's resource prefix '`my_prefix`'; rename to '`my_prefixTest`' ? !
  52. @Orbycius <resources> <string name="test">My other test</string> </resources> Resource named '`test`'

    does not start with the project's resource prefix '`my_prefix`'; rename to '`my_prefixTest`' ? !