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

Scalable applications: Make it Clean, make it S...

Scalable applications: Make it Clean, make it Simple

I spent the last 7 years thinking of what should be the best Android architecture in terms of simplicity and maintenance, in terms of excellence in the Android programming context. I teach this vision during all those previous years, providing trainings, conferences, books, tutorials, articles, Github projects.
And this year I faced an awesome challenge in terms of architecture for an important project that will impact billions of users in the future. I had a team of 12 full time developers to make it. We spent a year on it. And we make it scalable in term of features, developers’ team and end users.
The project is now used to create the next generation of Android applications for connected car for the biggest car company. It has two faces; an application and a SDK.
This is the perfect time for a feed back, what worked, what didn’t, why you should avoid Rx and focus on testable architecture. Are there different architectures between a SDK and an application? What are the concerns when creating a SDK? If your application can not be a SDK, did you miss something?
Let’s say, we’ll have a discussion, a deep one, about Android architecture.

Avatar for Mathias Seguy - Android2EE

Mathias Seguy - Android2EE

February 11, 2018
Tweet

More Decks by Mathias Seguy - Android2EE

Other Decks in Programming

Transcript

  1. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer
  2. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer
  3. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Babylon Knowledge Communication layer Memory Local Storage layer
  4. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Babylon Knowledge Communication layer Memory Local Storage layer
  5. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer
  6. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer
  7. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Makeup
  8. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Makeup View layer View layer is Responsible of • Displaying stuff • Following lifecycles • Grabing user’s actions
  9. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Model/Presenter layer is Responsible of managing data displayed by the view: • Update data according to view’s calls • Update views according to data changes Muscles Model/Presenter layer
  10. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Business Services layer is Responsible of implementing algorithms of your application. (All algorithms) Cortex Business Services layer
  11. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer DataStore layer is Responsible of implementing C.R.U.D. in your application in an abstract way. Conscious memory DataStore layer
  12. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer D.A.O. layer is Responsible of implementing C.R.U.D. in your application in the device. (Local storage) Memory Local Storage layer
  13. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Communication layer is Responsible of communicating with the rest of the world … Well this means making CRUD with external system. Babylon Knowledge Communication layer
  14. An Application ? Makeup View layer Muscles Model/Presenter layer Cortex

    Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Dependency (views depend on presenters,…)
  15. An Application Makeup View layer Muscles Model/Presenter layer Cortex Business

    Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Is simple
  16. A Jetpack Application Makeup View layer Muscles ViewModel Cortex Business

    Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Is simple LiveData Retrofit OkHttp/Okio/Moshi android.support.design android.support LiveData android.arch.lifecycle android.arch.persistence
  17. Scalable ? Nonsense, There is only one user at a

    time Scalable Android application ?
  18. Scalable ? How to be able to add thousands of

    features ? Android side Features scalability ?
  19. Scalable ? How to be able to grow from 3

    to 20 developers and more ? Android side. Scalable team?
  20. Scalable ? View layer Models layer Services layer Data layer

    fundation Use case 1 Use case 2 Use case 3 Use case 4 Use case 5 View layer Models layer Services layer Data layer
  21. Scalable ? Concretly Implement it with flavors or module dependencies

    1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature 1feature
  22. Background Threads , Main Thread Async Makeup View layer Muscles

    Model/Presenter layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer
  23. Background Threads , Main Thread Async Makeup View layer Muscles

    Model/Presenter layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Use AsyncTask every where ?
  24. Background Threads , Main Thread Async Makeup View layer Muscles

    Model/Presenter layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Use AsyncTask every where ?
  25. Background Threads Main Thread Async Makeup View layer Muscles Model/Presenter

    layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Be clear
  26. Background Threads Cortex Main Thread Async Muscles Model/Presenter layer Business

    Services layer Use events to return result Each method is available as synch and asynch Thread management is centralized Call methods Return result using events
  27. Background Threads Main Thread Async Muscles Model/Presenter layer Cortex Business

    Services layer Be clear Call methods Create a Runnable Do the job Take a Thread from the pool Return result using events
  28. Background Threads Main Thread Async Muscles Model/Presenter layer Cortex Business

    Services layer Be clear Create a Runnable Do the job Take a Thread from the pool Return result using events /** Should be called from the View */ @Override public void doAStuffAsync(Object object) { getExecutorServiceManager().execute(CANCELLABLE,new DoAStuffRunnable(object)); } Call methods
  29. Background Threads Main Thread Async Muscles Model/Presenter layer Cortex Business

    Services layer Call methods Do the job Take a Thread from the pool Return result using events private class DoAStuffRunnable implements Runnable { private object object; private DoAStuffRunnable(object object) { this.object = object; } @Override public void run() {doAStuff(object);} } Runnable, PoolExecutor, EventBus Create a Runnable
  30. Background Threads Main Thread Async Muscles Model/Presenter layer Cortex Business

    Services layer Call asynchronous methods Create a Runnable Take a Thread from the pool Return result using events @Override public GetDataEvent<Stuff> doAStuff(Object object) { GetDataEvent<Stuff> event=null; try { //make the job and create the event event = new GetDataEvent<Stuff>(BaseEvent.STATUS_SUCCESS, null, stuff, EventType.STUFF); } catch (Exception e) { event = new GetDataEvent<Stuff>(BaseEvent.STATUS_ERROR, Error.fromException(e), null, EventType.STUFF); } finally { eventBus.post(event); } return event; } Runnable, PoolExecutor, EventBus Do the job
  31. Background Threads Main Thread Async Muscles Model/Presenter layer Cortex Business

    Services layer Call methods Create a Runnable Take a Thread from the pool @Subscribe public void onEventStuff(GetDataEvent<Stuff> event) { //Update your data and the view } Runnable, PoolExecutor, EventBus Do the job Return result using events
  32. Background Threads Cortex Main Thread Async Muscles Model/Presenter layer No

    more Thread switch elsewhere Clean, centralized, simple, systematic Call asynchronous methods Return result using events Business Services layer
  33. Background Threads , Main Thread Async Makeup View layer Muscles

    Model/Presenter layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Call Return result using events
  34. More resources Chet Haase (Medium) Developing for Android Colt McAnlis

    (DevBytes Perfmatters) Android Performance Patterns Season 5 Mathias Seguy DroidConUk 2016 Threatment Architecture Thread Mathias Seguy DevoxxFr 2016 Architecture Android et bonnes pratiques
  35. How to test that ? Makeup View layer Muscles Model/Presenter

    layer Cortex Business Services layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer
  36. Testing Start by having a strategy Unit Tests Instrumented Tests

    Mock Production Don’t make meaningless tests You have 4 different contexts
  37. Testing First context UnitTest + Mock Unit Tests Business Services

    Test the service and the DataStore D.A.O DataStore Communication Mock Tests
  38. Testing Second context UnitTest + Prod Unit Tests Prod Communication

    layer Test the communication and the D.A.O. layer (integration tests) Local Storage layer Tests Tests use
  39. Testing Third context: Instrumented test + Mock Instrumented Tests Mock

    Model/Presenter Test the View layer Business Services Tests View
  40. Dependencies Injection So Dagger ! https://github.com/google/dagger Based on Module Provide

    I.O.C. Efficient Complexity drives to bugs I.O.C is a bad Android pattern A nuclear bomb to kill a fly
  41. Dependencies Injection Back to the goal D.A.O Communication Business Services

    ServerManagerMock DaoManagerMock ServiceManagerMoc k Business Services D.A.O. Communication ServerManager DaoManager ServiceManager Prod Mock
  42. Dependencies Injection Back to the goal D.A.O Communication Business Services

    ServerManagerMock DaoManagerMock ServiceManagerMoc k Business Services D.A.O. Communication ServerManager DaoManager ServiceManager Prod Mock
  43. Dependencies Injection Use flavors D.A.O Communication Business Services ServerManagerMock DaoManagerMock

    ServiceManagerMoc k Business Services D.A.O. Communication ServerManager DaoManager ServiceManager Prod Mock public class ServiceInjector { public static IServiceManager getServiceManager(){ if(BaseApplication.getInstance().isServiceManagerExists()){ return BaseApplication.getInstance().getServiceManager(); }else{ return new ServiceManagerMocked(); } } } public class ServiceInjector { public static IServiceManager getServiceManager(){ if(BaseApplication.getInstance().isServiceManagerExists()){ return BaseApplication.getInstance().getServiceManager(); }else{ return new ServiceManager(); } } }
  44. Dependencies Injection Use flavors D.A.O Communication Business Services ServerManagerMock DaoManagerMock

    ServiceManagerMoc k Business Services D.A.O. Communication ServerManager DaoManager ServiceManager Prod Mock
  45. Dependencies Injection Be scared of generic and systematic I.O.C. Have

    a fine grained control on which classes can be injected Make it yourself once Use flavors Using flavors has a side effect: you can build all the flavors Thank Chiuki: http://chiuki.github.io/
  46. SomeWhere in your code galaxy Manage exceptions Centralize the management

    Exception Manager ExceptionManager. manage(...) Team FeedBack User FeedBack
  47. Libraries Makeup View layer Muscles Model/Presenter layer Cortex Business Services

    layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Henson And Dart Retrofit OkHttp/Okio/Moshi android.support.design android.support EventBus android.arch.lifecycle android.arch.persistence And that’s all: Less you have better are you chance of success
  48. Libraries Makeup View layer Muscles Model/Presenter layer Cortex Business Services

    layer Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Henson And Dart Retrofit OkHttp/Okio/Moshi android.support.design android.support EventBus android.arch.lifecycle android.arch.persistence And that’s all: Less you have better are you chance of sucess You Shall Not Pass
  49. Libraries And that’s all: Less you have better are you

    chance of sucess Good luck with your devs…
  50. Libraries And that’s all: Less you have better are you

    chance of sucess Good luck with your devs… …but stay paranoïac with libs and read this : I’m harvesting credit card numbers and passwords from your site. Here’s how. https://hackernoon.com/im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8cb347c5b5
  51. Where is the SDK ? View layer Models layer Services

    layer Data layer fundation Use case 1 Use case 2 Use case 3 Use case 4 Use case 5 View layer Models layer Services layer Data layer
  52. This is your SDK View layer Models layer Services layer

    Data layer fundation Use case 1 Use case 2 Use case 3 Use case 4 Use case 5 View layer Models layer Services layer Data layer
  53. This is your SDK View layer Models layer Services layer

    Data layer fundation Exception Management Dependency injection OAuth Request interceptor and RetrofitBuilder PoolExecutors Management 1 second pattern Base View/Activity/ViewModel Base Business services Base events Custom toast/dialog/views Base Application Object Style It contains all your architecture pattern
  54. Use Gradle extensively You need tools to analyse your code

    No Tests, No Reporting Tools, No big vision reports ?
  55. Use Gradle extensively You need tools to analyse your code

    No Tests, No Reporting Tools, No big vision reports ? You are dead
  56. Makeup View layer Muscles Model/Presenter layer Cortex Business Services layer

    Memory Local Storage layer Babylon Knowledge Communication layer Conscious memory DataStore layer Keep it simple
  57. Keep it simple Make it scalable fundation Use case 1

    Use case 2 Use case 3 Use case 4 Use case 5 View layer Models layer Services layer Data layer View layer Models layer Services layer Data layer
  58. Keep it simple Make it scalable Manage Asynchronicity Background Threads

    Main Thread Muscles Model/Presenter layer Cortex Business Services layer Call Return result using events
  59. Keep it simple Make it scalable Manage Asynchronicity Test it

    Unit Tests Instrumented Tests Mock Production
  60. Keep it simple Make it scalable Manage Asynchronicity Test it

    Manage issues Exception Manager Team FeedBack User FeedBack
  61. Keep it simple Make it scalable Manage Asynchronicity Test it

    Manage issues Have strong fundations fundation View layer Models layer Services layer Data layer
  62. Keep it simple Make it scalable Manage Asynchronicity Test it

    Manage issues Have strong fundations Fears lib
  63. Keep it simple Make it scalable Manage Asynchronicity Test it

    Manage issues Have strong fundations Fears lib Use reporting tools
  64. It takes energy to create excellence. It takes time, It

    needs convictions. If it was easy, We would all have several Phd.