Clean Architecture on Android
Benjamin / 班傑明
A real-world app
Fragment Adapter
AsyncTask
Network API client
AsyncTask
View
View
Data model
Challenge
● Hard to understand/maintain
○ The business logic is mixed in Activity/Fragment/Adapter
○ Loose cohesive, high coupled (低內聚,高耦合)
● Hard to test
○ Cannot fully test the logic in Activity/Fragment/Adapter
○ The UI testing is fragile. (Will build fail if there are content or server-side issues)
○ It costs around 30 minutes to finish our UI testing
○ It’s really frustrating and lose productivity when the tests keep failing (lots of 30+ minutes)
MVP
From: http://www.captechconsulting.com/blogs/a-mvp-approach-to-lifecycle-safe-requests-with-retrofit-20-and-rxjava
MVP - Interfaces
Program to an interface, not an implementation
MVP - View
MVP - View
MVP - Presenter
MVP - Model
● Model? NetworkAPIClient?
● Is MVP an Architecture pattern?
MVC/MVP/MVVM is NOT an Architecture
● MVP is not an architectural pattern, it’s only responsible
for the presentation layer (delivery mechanism)
The Clean Architecture
From: http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
The Clean Architecture (conti.)
From Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg
Interactor - Application specific business rules
● Application specific business rules
● Use cases
● ex: Content aggregation logic for your
app’s landing page, add product to cart, ...
● Regular Java object. No android
framework dependency
Repository - Data access
● Application independent business rules
● Business object / Domain model
● Access data by network, memory cache
and disk cache, has Android framework
dependency
● ex: RestAPI, Preference, disk cache
Boundary
● No direct dependency between
framework and Interactor
● Interactor can be well tested without
framework
MVP - Presentation Layer
● Presenter: Define interfaces for all the UI
interactions [Regular Java object, no
android framework dependency]
The Clean Architecture on Android
Fragment Adapter
Network API client
View
View
Presenter Interactor RepositoryViewModel
DataCache
Response
Model
The Clean Architecture
● Independent of Frameworks
● Testable
● Allow major decisions(UI, framework, databases) to be
deferred
Android application architecture: Get ready for the next
billion! - Google I/O 2016
From https://www.youtube.com/watch?v=70WqJxymPr8
Testing
● Presentation layer
○ Expresso
● Domain layer (Use case/Interactor)
○ JUnit + mockito
● Data layer
○ Roboletric + JUnit + mockito
Prod
build flavor
Expresso + Mockito
Fragment AdapterView
View
Presenter
InjectionInteractor
Repository
Expresso + Mockito (conti.)
Mock
build flavor
Fragment AdapterView
View
Presenter
InjectionMockInteractor
Repository
Expresso + Mockito (conti.)
Reference
● Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg
● Architecting Android…The clean way? http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/
● Android Application Architecture. Our journey from standard Activities and AsyncTasks to a modern MVP-based architecture
powered by RxJava. https://labs.ribot.co.uk/android-application-architecture-8b6e34acda65#.zexgfy5tt
● Android Testing (Android Dev Summit 2015) https://www.youtube.com/watch?v=vdasFFfXKOY
● Android Testing Codelab https://codelabs.developers.google.com/codelabs/android-testing/index.html#0
● Advanced Android Espresso http://chiuki.github.io/advanced-android-espresso/#/

Clean architecture on android

  • 1.
    Clean Architecture onAndroid Benjamin / 班傑明
  • 2.
    A real-world app FragmentAdapter AsyncTask Network API client AsyncTask View View Data model
  • 3.
    Challenge ● Hard tounderstand/maintain ○ The business logic is mixed in Activity/Fragment/Adapter ○ Loose cohesive, high coupled (低內聚,高耦合) ● Hard to test ○ Cannot fully test the logic in Activity/Fragment/Adapter ○ The UI testing is fragile. (Will build fail if there are content or server-side issues) ○ It costs around 30 minutes to finish our UI testing ○ It’s really frustrating and lose productivity when the tests keep failing (lots of 30+ minutes)
  • 4.
  • 5.
    MVP - Interfaces Programto an interface, not an implementation
  • 6.
  • 7.
  • 8.
  • 9.
    MVP - Model ●Model? NetworkAPIClient? ● Is MVP an Architecture pattern?
  • 10.
    MVC/MVP/MVVM is NOTan Architecture ● MVP is not an architectural pattern, it’s only responsible for the presentation layer (delivery mechanism)
  • 11.
    The Clean Architecture From:http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
  • 12.
    The Clean Architecture(conti.) From Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg
  • 13.
    Interactor - Applicationspecific business rules ● Application specific business rules ● Use cases ● ex: Content aggregation logic for your app’s landing page, add product to cart, ... ● Regular Java object. No android framework dependency
  • 14.
    Repository - Dataaccess ● Application independent business rules ● Business object / Domain model ● Access data by network, memory cache and disk cache, has Android framework dependency ● ex: RestAPI, Preference, disk cache
  • 15.
    Boundary ● No directdependency between framework and Interactor ● Interactor can be well tested without framework
  • 16.
    MVP - PresentationLayer ● Presenter: Define interfaces for all the UI interactions [Regular Java object, no android framework dependency]
  • 17.
    The Clean Architectureon Android Fragment Adapter Network API client View View Presenter Interactor RepositoryViewModel DataCache Response Model
  • 18.
    The Clean Architecture ●Independent of Frameworks ● Testable ● Allow major decisions(UI, framework, databases) to be deferred
  • 19.
    Android application architecture:Get ready for the next billion! - Google I/O 2016 From https://www.youtube.com/watch?v=70WqJxymPr8
  • 20.
    Testing ● Presentation layer ○Expresso ● Domain layer (Use case/Interactor) ○ JUnit + mockito ● Data layer ○ Roboletric + JUnit + mockito
  • 21.
    Prod build flavor Expresso +Mockito Fragment AdapterView View Presenter InjectionInteractor Repository
  • 22.
    Expresso + Mockito(conti.) Mock build flavor Fragment AdapterView View Presenter InjectionMockInteractor Repository
  • 23.
  • 24.
    Reference ● Robert CMartin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg ● Architecting Android…The clean way? http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/ ● Android Application Architecture. Our journey from standard Activities and AsyncTasks to a modern MVP-based architecture powered by RxJava. https://labs.ribot.co.uk/android-application-architecture-8b6e34acda65#.zexgfy5tt ● Android Testing (Android Dev Summit 2015) https://www.youtube.com/watch?v=vdasFFfXKOY ● Android Testing Codelab https://codelabs.developers.google.com/codelabs/android-testing/index.html#0 ● Advanced Android Espresso http://chiuki.github.io/advanced-android-espresso/#/

Editor's Notes

  • #5 From http://www.captechconsulting.com/blogs/a-mvp-approach-to-lifecycle-safe-requests-with-retrofit-20-and-rxjava
  • #12 From http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
  • #18 Presenter: define interfaces for all the UI interactions [Regular Java object, no android framework dependency] Inteactor: Business logic [Regular Java object, no android framework dependency] Repository: Access data by network, memory cache and disk cache [Android framework dependency]
  • #20 From http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html