clean architecture on android
TRANSCRIPT
Clean Architecture on AndroidBenjamin / 班傑明
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.)
Mockbuild 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/#/