Slide 1

Slide 1 text

Android Application Development: Better, Faster, Stronger!

Slide 2

Slide 2 text

Filip maelbrancke TWITTER: @fmaelbrancke EMAIL: [email protected] YOUR HOST EMAIL: [email protected] consultant @ AppFoundry

Slide 3

Slide 3 text

Why this session?

Slide 4

Slide 4 text

First off... Why? What? How?

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Avoid problems

Slide 11

Slide 11 text

You = McGuyver

Slide 12

Slide 12 text

Deployment pipeline Commit stage Automated test/QA stage Manual QA testing UAT test Production Capacity / load testing

Slide 13

Slide 13 text

Build pipeline Checkout / compile Unit tests Test coverage Code analysis Create deployable artifact Deploy for automatic QA test Trigger automated QA stage

Slide 14

Slide 14 text

IDE Eclipse ! IntelliJ ! Android Studio

Slide 15

Slide 15 text

Build Ant ! Maven ! Gradle

Slide 16

Slide 16 text

CI Jenkins ! Atlassian Bamboo ! Travis ! Teamcity

Slide 17

Slide 17 text

Testing JUnit / TestNG ! Mocking ! UI testing

Slide 18

Slide 18 text

Build system

Slide 19

Slide 19 text

Android project setup & build ant gradle

Slide 20

Slide 20 text

Android SDK build system

Slide 21

Slide 21 text

Gradle? why - goals Powerful build system declarative, flexible tooling API ! Unified across IDEs & CI servers ! Free / open source

Slide 22

Slide 22 text

Basics buildscript { ! repositories { ! mavenCentral() ! } ! dependencies { ! classpath ‘com.android.tools.build:gradle:0.11.+’ ! }! }! ! apply plugin: ‘android’! ! android { ! compileSdkVersion 19 ! buildToolsVersion ‘19.1.0’! }! !

Slide 23

Slide 23 text

Gradle tasks assemble debug / release ! check (test) check / connectedCheck ! build

Slide 24

Slide 24 text

Build types build / packaging customization debuggable flag ProGuard signing configuration source / resources overlay debug and release prebuilt

Slide 25

Slide 25 text

local dependencies dependencies { compile fileTree(dir: ‘libs’, include: ‘*.jar’) } android { ... }

Slide 26

Slide 26 text

remote dependencies repositories { mavenCentral() } android { ... } dependencies { instrumentTestCompile ‘com.squareup:fest-android:1.0.4’ compile file(‘libs/protobuf.jar’) }

Slide 27

Slide 27 text

multi-project setup MyProject/ | settings.gradle + app/ | build.gradle + libraries/ + lib1/ | build.gradle + lib2/ | build.gradle

Slide 28

Slide 28 text

android library Binary Bundle (.aar) - Uploadable to repositories Support for - assets - ProGuard rules - Custom Lint Rules - ...

Slide 29

Slide 29 text

build variants build variants product flavors

Slide 30

Slide 30 text

Device vs emulator Genymotion

Slide 31

Slide 31 text

Testing

Slide 32

Slide 32 text

Testing Manual testing is tedious ! Humans are not very good at testing ! But computers are...

Slide 33

Slide 33 text

“Computers are designed to do simple repetitive tasks. The second you have humans doing repetitive tasks, all the computers get together late at night and laugh at you...” Neil Ford

Slide 34

Slide 34 text

Manual vs Automated

Slide 35

Slide 35 text

Manual vs Automated

Slide 36

Slide 36 text

Early detection

Slide 37

Slide 37 text

Excuses We never make mistakes! The functionality is trivial Tests slow us down Management won’t let us

Slide 38

Slide 38 text

Unit test Isolated Repeatable Fast Self-documenting

Slide 39

Slide 39 text

Repeatability Tests need to be run by every developer (no matter what development stack he uses) Tests must not rely on the environment in which they are being run

Slide 40

Slide 40 text

Speed The shorter, the less distraction, less of an interruption to our workflow

Slide 41

Slide 41 text

Self documenting Testable code is clear and easy to follow No need to explain how a certain component works, we can just look at the test No need to write documentation

Slide 42

Slide 42 text

Self documenting Testable code is clear and easy to follow No need to explain how a certain component works, we can just look at the test No need to write documentation Tests = usage examples

Slide 43

Slide 43 text

Testing Automated testing

Slide 44

Slide 44 text

(Unit) testing in Android Android SDK (JUnit 3-based) Instrumentation tests ! Monkeyrunner The monkey UIAutomator + UIAutomatorViewer

Slide 45

Slide 45 text

Android instrumentation tests JUnit 3 ! import junit.framework.*; public class MyTestClass extends TestCase { public void testMethodName() throws Exception { } }

Slide 46

Slide 46 text

Android instrumentation tests Instrumentation = set of control methods or “hooks” in the Android system ! These hooks control an Android component independently of the normal lifecycle

Slide 47

Slide 47 text

Android instrumentation tests Application Activity Service Content Provider

Slide 48

Slide 48 text

Android instrumentation tests Isolate tests from the system: MockApplication MockContext MockResources MockContentProvider MockContentResolver MockPackageManager

Slide 49

Slide 49 text

Exercise

Slide 50

Slide 50 text

The monkey Generates pseudo-random stream of user events: clicks touches gestures system-level events ! stress test your app

Slide 51

Slide 51 text

The monkey command-line tool Basic configuration options, such as setting the number of events to attempt. Operational constraints, such as restricting the test to a single package. Event types and frequencies. Debugging options. ! $ adb shell monkey -p your.package.name -v 500 http://developer.android.com/tools/help/monkey.html

Slide 52

Slide 52 text

Exercise

Slide 53

Slide 53 text

UI testing UIAutomator ! ! ! ! Espresso

Slide 54

Slide 54 text

Espresso Android UI test API ! © 2013 ! https://code.google.com/p/android-test-kit/ ! !

Slide 55

Slide 55 text

Espresso Easier setup ! Fluid API ! Faster ! Extensible

Slide 56

Slide 56 text

Espresso Code ! onView(withId(R.id.x)).perform(click())
 
 onView(withId(R.id.x)).check(matches(withText(containsString(“x”))))

Slide 57

Slide 57 text

Third Party Tools Hamcrest

Slide 58

Slide 58 text

Robelectric Android core libraries depend upon the actual Android operating system

Slide 59

Slide 59 text

Robolectric AndroidTestCase needs an instance of the emulator to run ! Big time sink: - spin up emulator - deploy the APK - run the actual tests

Slide 60

Slide 60 text

Robolectric Replaces the behavior of code that would otherwise need an emulator / device Write JUnit tests without the baggage of a device ! Does this by using Shadow Classes (mock implementations of Android core libraries)

Slide 61

Slide 61 text

Exercise

Slide 62

Slide 62 text

Roboelectric InstrumentationTestCase too slow / hard for unit testing? ! vs real device / speedy emulator

Slide 63

Slide 63 text

Roboelectric advantages Unit testing fast and easy Make your own shadow objects JUnit 4 supported (Android = JUnit 3)

Slide 64

Slide 64 text

Roboelectric disadvantages Does not cover all functionality (sensors, OpenGL, ...)
 → device needed Integration testing (interaction Activities - Services, camera app, ...)

Slide 65

Slide 65 text

Build system

Slide 66

Slide 66 text

Android project setup & build ant gradle

Slide 67

Slide 67 text

Android build system

Slide 68

Slide 68 text

Android Maven plugin https://code.google.com/p/maven-android-plugin/ com.jayway.maven.plugins.android.generation2 android-maven-plugin 3.8.0 !

Slide 69

Slide 69 text

Maven “Software project management and comprehension tool” ! Builds your software and much more ! De-facto standard for Java software builds ! Convention over configuration ! Declarative rather than procedural

Slide 70

Slide 70 text

Maven and Android Android Maven Plugin – core tool ! M2Eclipse, m2e-android – for Eclipse users ! Maven Android SDK Deployer – for SDK libraries ! Maven Android Archetype – project templates

Slide 71

Slide 71 text

Gradle vs Maven Gradle Maven Build variants / product flavors ✔ ✖ Android test framework ✔ ✔ Robotium ✔ ✔ Roboelectric ✔ ✔ UIAutomator / Monkey ✖ ✔ Code coverage ✖ ✔ Code quality (pmd, checkstyle, findbugs, lint) ✔ ✔ Aggregation ✔ ✔

Slide 72

Slide 72 text

Mocks Easymock ! Mockito - uses DexMaker to generate mock classes

Slide 73

Slide 73 text

Mocking Mocking Frameworks allow us to test the code you want, without its dependencies. Mock objects can simulate the behaviour of complex objects. Mock objects isolate the unit of code you are testing.

Slide 74

Slide 74 text

Mockito Prepare the mock + behavior ! Test the method of interest ! Validate that the mock saw what we expected / didn’t see anything unexpected

Slide 75

Slide 75 text

Exercise

Slide 76

Slide 76 text

Robotium Extension of the Android testing framework Makes it easier to write UI tests Inherits from ActivityInstrumentationTestCase2 Main class for testing is Solo Solo is initialized with the instrumentation of the testcase and the activity to test https://code.google.com/p/robotium/

Slide 77

Slide 77 text

Exercise

Slide 78

Slide 78 text

Android FEST Syntactic sugar Extension of the FEST library Fluent syntax for checking assertions Makes tests easier to read/write ! https://github.com/square/fest-android

Slide 79

Slide 79 text

Android FEST assertEquals(View.GONE, view.getVisibility()); assertThat(view).isGone(); expected: <8> but was: <4> Expected visibility but was .

Slide 80

Slide 80 text

Android FEST assertEquals(View.GONE, view.getVisibility()); assertThat(view).isGone(); expected: <8> but was: <4> Expected visibility but was . JUNIT ANDROID FEST

Slide 81

Slide 81 text

Android FEST JUNIT ANDROID FEST assertEquals(View.VISIBLE, layout.getVisibility()); assertEquals(VERTICAL, layout.getOrientation()); assertEquals(4, layout.getChildCount()); assertEquals(SHOW_DIVIDERS_MIDDLE, layout.getShowDividers()); assertThat(layout).isVisible() .isVertical() .hasChildCount(4) .hasShowDividers(SHOW_DIVIDERS_MIDDLE);

Slide 82

Slide 82 text

Exercise

Slide 83

Slide 83 text

Spoon Automate test execution across multiple devices ! Aggregate the results ! Aggregation of screenshots http://square.github.io/spoon/

Slide 84

Slide 84 text

Spoon: overview

Slide 85

Slide 85 text

Spoon: device view

Slide 86

Slide 86 text

Spoon: test view

Slide 87

Slide 87 text

Spoon: screenshots Visual inspection of tests execution Spoon.screenshot(activity, "initial_state"); /* Normal test code... */ Spoon.screenshot(activity, "after_login");

Slide 88

Slide 88 text

Exercise

Slide 89

Slide 89 text

Behavior Driven Development

Slide 90

Slide 90 text

(Unit) testing in Android Android SDK (JUnit 3-based) Instrumentation tests ! !

Slide 91

Slide 91 text

Third party libraries Robotium RoboElectric Mockito Fest Android Spoon

Slide 92

Slide 92 text

Cloud testing

Slide 93

Slide 93 text

A better Android emulator Automate testing ! Dozens of devices - different screen sizes - different Android versions

Slide 94

Slide 94 text

Manymo

Slide 95

Slide 95 text

Android Gradle integration

Slide 96

Slide 96 text

CI

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

Continuous integration benefits Fast feedback - fewer errors Test everything on every (nightly) build Less manual testing Regression tests without additional effort

Slide 99

Slide 99 text

Android

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Other

Slide 103

Slide 103 text

Genymotion http://blog.genymobile.com/genymotion-jenkins-android-testing/

Slide 104

Slide 104 text

Repository manager

Slide 105

Slide 105 text

Architecture Simple architecture ! Dependency injection

Slide 106

Slide 106 text

Code quality

Slide 107

Slide 107 text

Measure...

Slide 108

Slide 108 text

Quality Tools for Android https://github.com/stephanenicolas/Quality- Tools-for-Android

Slide 109

Slide 109 text

Quality Tools for Android

Slide 110

Slide 110 text

Build pipeline tools Build (maven - gradle) Dependency repo (nexus - artifactory) Testing framework (JUnit - ...) Test coverage (Cobertura - Emma - Jacoco) Code analysis (Checkstyle, findbugs, pmd, Android Lint) Creation of deployable artifact (buildtool, artifact repo) Trigger next stage

Slide 111

Slide 111 text

Better Android apps

Slide 112

Slide 112 text

References

Slide 113

Slide 113 text

Suggested reading Android Application Testing Guide Diego Torres Milano (9781849513500) Robotium Automated Testing for Android 
 Hrushikesh Zadgaonkar (9781782168010) ! 


Slide 114

Slide 114 text

Suggested reading Test Driven Development: By Example Beck, Kent (978-0321146533) Continuous Integration: Improving 
 Software Quality and Reducing Risk 
 Duvall, Paul M. et al. (978-0321336385) Working Effectively with Legacy Code
 Feathers, Michael (978-0131177055) 


Slide 115

Slide 115 text

Suggested viewing Google IO sessions

Slide 116

Slide 116 text

Q + A

Slide 117

Slide 117 text

Filip maelbrancke TWITTER: @fmaelbrancke EMAIL: [email protected] THANK YOU EMAIL: [email protected] consultant @ AppFoundry