Clean architecture: Android Michał Szczepanik
10
Our goal(s)
● Architecture focused on use cases and main system function
● Clear use cases first!● Easy to maintain● Fast to test● Easy to test● Very cohesive● Decoupled
17
Use Cases
Use Cases orchestrate the flow of data to and from the entities.
They are also called Interactors.
18
Interface Adapters
This set of adapters convert data from the format most convenient for the use cases and entities.
Presenters and Controllers belong here.
24
App architecture – developer view
Presentation Layer
Domain/BusinessLayer
DataLayer
Entities Layer
Inte
ract
ors
Boun
d aries
25
Domain (Business) Layer
● Application business rules● All use cases (interactors)
● Android independent
26
Domain (Business) Layer
Domain/BusinessLayer
Entities LayerInte
ract
ors
Inte
rfa
ces
Repos itory (D
ata access) In terfa ce
● Interactors Implementation● Use entities objects
● Most tests are here● (JUnit plus mockito)
27
Entities Layer
● Basic (minimal) business object● Object's structure● Low-level rules ● General truths
● Android independent● Framework independent
28
Entities Layer
● Basic java object● No frameworks
● Test for business objects● No mocks and stubs
Domain/BusinessLayer
Entities Layer
Inte
ract
ors
Bound
aries
29
Presentation Layer
● Logic related to UI (view and animation)● Logic related to Activity and application lifecycle● Fragments and Activities are only views!● Design: MVP/MVC/MVVM pattern
● Strongly connected with Android SDK
30
Presentation Layer
● Frameworks
● Integration (platform) tests● UI tests● (Android Instrumentation)● (Robotium/Espresso)● (Robolectric)
Presentation Layer
Inte
ract
ors
32
Data Layer● Repository Pattern
● Integration tests● Unit tests● (JUnit plus mockito)● (Robolectric)
Implementation
Repos ito
ry Interf ace
Memory(cache)
Disk(DB/File)
Cloud
34
Testing
● Low-level tests are fast ● High-level tests are slow
● UI testing sucks ?● Self testing code ?
● new bug = new test
38
65K
Unable to execute dex: method ID not in [0, 0xffff]: 65536
Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536
41
Dalvik / ART
● Dalvik:– Register-based machine
– Just in time (JIT): 2.3+
● ART– Ahead-of-time (AOT)
43
Lint
● Static Code Analysis● Layouts tips and optimization
● Missing translations ● Layout performance problems ● Unused resources● Inconsistent array sizes ● Accessibility and internationalization problems ● Icon problems ● Usability problems ● Manifest errors
46
Injectionsclass ExampleActivity extends Activity {
@InjectView(R.id.title) TextView title;
@InjectView(R.id.subtitle) TextView subtitle;
@InjectView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}
50
WroDroidhttp://www.meetup.com/WroDroid/
Clean Architecture by Unkle Bobhttp://goo.gl/Z4zMHG
Architecting Android…The clean wayhttp://goo.gl/MF13E1
Effective Android UIhttp://goo.gl/1dZxfb