Top Banner
Clean architecture: Android Michał Szczepanik
52

Clean architecture: Android

Aug 06, 2015

Download

Technology

BLStream
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clean architecture: Android

Clean architecture: Android

Michał Szczepanik

Page 2: Clean architecture: Android

WroDroid

Page 3: Clean architecture: Android

3

Page 4: Clean architecture: Android

4

Page 5: Clean architecture: Android

5

Page 6: Clean architecture: Android

6

I have a Dream...

Page 7: Clean architecture: Android

7

What a Terrible Failure!

Page 8: Clean architecture: Android

8

What is Architecture?

Page 9: Clean architecture: Android

9

Architecture is About Intent, not Frameworks„

Page 10: Clean architecture: Android

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

Page 11: Clean architecture: Android

11

Step 0 (setup?)

Page 12: Clean architecture: Android

12

Architect role

Page 13: Clean architecture: Android

13

Team Role

Page 14: Clean architecture: Android

14Android best practices ?

Page 15: Clean architecture: Android

15

Page 16: Clean architecture: Android

16

Entities

The business objects of the application

Page 17: Clean architecture: Android

17

Use Cases

Use Cases orchestrate the flow of data to and from the entities.

They are also called Interactors.

Page 18: Clean architecture: Android

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.

Page 19: Clean architecture: Android

19

Frameworks and Drivers

This is where all the details go: UI, tools, frameworks, etc.

Page 20: Clean architecture: Android

20

Page 21: Clean architecture: Android

21

How to be clean ?

Page 22: Clean architecture: Android

22

Page 23: Clean architecture: Android

23

App architecture – android view

Page 24: Clean architecture: Android

24

App architecture – developer view

Presentation Layer

Domain/BusinessLayer

DataLayer

Entities Layer

Inte

ract

ors

Boun

d aries

Page 25: Clean architecture: Android

25

Domain (Business) Layer

● Application business rules● All use cases (interactors)

● Android independent

Page 26: Clean architecture: Android

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)

Page 27: Clean architecture: Android

27

Entities Layer

● Basic (minimal) business object● Object's structure● Low-level rules ● General truths

● Android independent● Framework independent

Page 28: Clean architecture: Android

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

Page 29: Clean architecture: Android

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

Page 30: Clean architecture: Android

30

Presentation Layer

● Frameworks

● Integration (platform) tests● UI tests● (Android Instrumentation)● (Robotium/Espresso)● (Robolectric)

Presentation Layer

Inte

ract

ors

Page 31: Clean architecture: Android

31

Data Layer

● Deliver data needed by app● Cache● Service● Design: with Repository Pattern

Page 32: Clean architecture: Android

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

Page 33: Clean architecture: Android

33

Page 34: Clean architecture: Android

34

Testing

● Low-level tests are fast ● High-level tests are slow

● UI testing sucks ?● Self testing code ?

● new bug = new test

Page 35: Clean architecture: Android

35

QA Team

Page 36: Clean architecture: Android

36

Monkey

adb shell monkey [options] <event-count>

-s <seed>

-v <verbosity level>

Page 37: Clean architecture: Android

37

Android Tips

Page 38: Clean architecture: Android

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

Page 39: Clean architecture: Android

39

How to solve it

● Find reason– dex-method-counts

● jarjar● Shrinking libs via Proguard● Multidex

Page 40: Clean architecture: Android

40

Page 41: Clean architecture: Android

41

Dalvik / ART

● Dalvik:– Register-based machine

– Just in time (JIT): 2.3+

● ART– Ahead-of-time (AOT)

Page 42: Clean architecture: Android

42

Tools?

Page 43: Clean architecture: Android

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

Page 44: Clean architecture: Android

44

Dependency Injections

● Dagger

● RoboGuice

Page 45: Clean architecture: Android

45

Injections● ButterKnife

● AndroidAnnotations

Page 46: Clean architecture: Android

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...

}

}

Page 47: Clean architecture: Android

47

Proguard / Dexguard

● Shrinking● Optimize ● Obfuscate

Page 48: Clean architecture: Android

48

Developer options

Page 49: Clean architecture: Android

49

Clean Architecture

● Easy to maintain● Easy to test● Very cohesive● Decoupled

Page 50: Clean architecture: Android

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

Page 51: Clean architecture: Android

51

Thank you for your attention

Page 52: Clean architecture: Android

52

Questions ?

[email protected]