Clean architecture: Android
Michał Szczepanik
WroDroid
3
4
5
6
I have a Dream...
7
What a Terrible Failure!
8
What is Architecture?
9
Architecture is About Intent, not Frameworks
„
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
11
Step 0 (setup?)
12
Architect role
13
Team Role
14
Android best practices ?
15
16
Entities
The business objects of the application
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.
19
Frameworks and Drivers
This is where all the details go: UI, tools,
frameworks, etc.
20
21
How to be clean ?
22
23
App architecture – android view
24
App architecture – developer view
Presentation
Layer
Domain/Business
Layer
Data
Layer
Entities Layer
Interactors
Boundaries
25
Domain (Business) Layer
● Application business rules
● All use cases (interactors)
● Android independent
26
Domain (Business) Layer
Domain/Business
Layer
Entities Layer
InteractorsInterfaces
Repository(Dataaccess)Interface
● 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/Business
Layer
Entities Layer
Interactors
Boundaries
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
Interactors
31
Data Layer
● Deliver data needed by app
● Cache
● Service
● Design: with Repository Pattern
32
Data Layer
● Repository Pattern
● Integration tests
● Unit tests
● (JUnit plus mockito)
● (Robolectric)
Implementation
RepositoryInterface
Memory
(cache)
Disk
(DB/File)
Cloud
33
34
Testing
● Low-level tests are fast
● High-level tests are slow
● UI testing sucks ?
● Self testing code ?
● new bug = new test
35
QA Team
36
Monkey
adb shell monkey [options] <event-count>
-s <seed>
-v <verbosity level>
37
Android Tips
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
39
How to solve it
● Find reason
– dex-method-counts
● jarjar
● Shrinking libs via Proguard
● Multidex
40
41
Dalvik / ART
● Dalvik:
– Register-based machine
– Just in time (JIT): 2.3+
● ART
– Ahead-of-time (AOT)
42
Tools?
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
44
Dependency Injections
● Dagger
● RoboGuice
45
Injections
● ButterKnife
● AndroidAnnotations
46
Injections
class 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...
}
}
47
Proguard / Dexguard
● Shrinking
● Optimize
● Obfuscate
48
Developer options
49
Clean Architecture
● Easy to maintain
● Easy to test
● Very cohesive
● Decoupled
50
WroDroid
http://www.meetup.com/WroDroid/
Clean Architecture by Unkle Bob
http://goo.gl/Z4zMHG
Architecting Android…The clean way
http://goo.gl/MF13E1
Effective Android UI
http://goo.gl/1dZxfb
51
Thank you for your attention
52
Questions ?
michal.szczepanik@blstream.com

Clean architecture: Android