Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Application Development: Better, Faster, Stronger! at Devoxx UK 2014

Android Application Development: Better, Faster, Stronger! at Devoxx UK 2014

Android Application Development hands-on lab @ Devoxx UK 2014

http://www.devoxx.co.uk/2014/05/android-application-development-better-faster-stronger/

103e1ebcacd620770cf32a36b9aba17e?s=128

AppFoundry

June 13, 2014
Tweet

Transcript

  1. Android Application Development: Better, Faster, Stronger!

  2. Filip maelbrancke TWITTER: @fmaelbrancke EMAIL: filip@maelbrancke.net YOUR HOST EMAIL: info@appfoundry.be

    consultant @ AppFoundry
  3. Why this session?

  4. First off... Why? What? How?

  5. None
  6. None
  7. None
  8. None
  9. None
  10. Avoid problems

  11. You = McGuyver

  12. Deployment pipeline Commit stage Automated test/QA stage Manual QA testing

    UAT test Production Capacity / load testing
  13. Build pipeline Checkout / compile Unit tests Test coverage Code

    analysis Create deployable artifact Deploy for automatic QA test Trigger automated QA stage
  14. IDE Eclipse ! IntelliJ ! Android Studio

  15. Build Ant ! Maven ! Gradle

  16. CI Jenkins ! Atlassian Bamboo ! Travis ! Teamcity

  17. Testing JUnit / TestNG ! Mocking ! UI testing

  18. Build system

  19. Android project setup & build ant gradle

  20. Android SDK build system

  21. Gradle? why - goals Powerful build system declarative, flexible tooling

    API ! Unified across IDEs & CI servers ! Free / open source
  22. Basics buildscript { ! repositories { ! mavenCentral() ! }

    ! dependencies { ! classpath ‘com.android.tools.build:gradle:0.11.+’ ! }! }! ! apply plugin: ‘android’! ! android { ! compileSdkVersion 19 ! buildToolsVersion ‘19.1.0’! }! !
  23. Gradle tasks assemble debug / release ! check (test) check

    / connectedCheck ! build
  24. Build types build / packaging customization debuggable flag ProGuard signing

    configuration source / resources overlay debug and release prebuilt
  25. local dependencies dependencies { compile fileTree(dir: ‘libs’, include: ‘*.jar’) }

    android { ... }
  26. remote dependencies repositories { mavenCentral() } android { ... }

    dependencies { instrumentTestCompile ‘com.squareup:fest-android:1.0.4’ compile file(‘libs/protobuf.jar’) }
  27. multi-project setup MyProject/ | settings.gradle + app/ | build.gradle +

    libraries/ + lib1/ | build.gradle + lib2/ | build.gradle
  28. android library Binary Bundle (.aar) - Uploadable to repositories Support

    for - assets - ProGuard rules - Custom Lint Rules - ...
  29. build variants build variants product flavors

  30. Device vs emulator Genymotion

  31. Testing

  32. Testing Manual testing is tedious ! Humans are not very

    good at testing ! But computers are...
  33. “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
  34. Manual vs Automated

  35. Manual vs Automated

  36. Early detection

  37. Excuses We never make mistakes! The functionality is trivial Tests

    slow us down Management won’t let us
  38. Unit test Isolated Repeatable Fast Self-documenting

  39. 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
  40. Speed The shorter, the less distraction, less of an interruption

    to our workflow
  41. 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
  42. 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
  43. Testing Automated testing

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

    ! Monkeyrunner The monkey UIAutomator + UIAutomatorViewer
  45. Android instrumentation tests JUnit 3 ! import junit.framework.*; public class

    MyTestClass extends TestCase { public void testMethodName() throws Exception { } }
  46. 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
  47. Android instrumentation tests Application Activity Service Content Provider

  48. Android instrumentation tests Isolate tests from the system: MockApplication MockContext

    MockResources MockContentProvider MockContentResolver MockPackageManager
  49. Exercise

  50. The monkey Generates pseudo-random stream of user events: clicks touches

    gestures system-level events ! stress test your app
  51. 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
  52. Exercise

  53. UI testing UIAutomator ! ! ! ! Espresso

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

    ! !
  55. Espresso Easier setup ! Fluid API ! Faster ! Extensible

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

  57. Third Party Tools Hamcrest

  58. Robelectric Android core libraries depend upon the actual Android operating

    system
  59. Robolectric AndroidTestCase needs an instance of the emulator to run

    ! Big time sink: - spin up emulator - deploy the APK - run the actual tests
  60. 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)
  61. Exercise

  62. Roboelectric InstrumentationTestCase too slow / hard for unit testing? !

    vs real device / speedy emulator
  63. Roboelectric advantages Unit testing fast and easy Make your own

    shadow objects JUnit 4 supported (Android = JUnit 3)
  64. Roboelectric disadvantages Does not cover all functionality (sensors, OpenGL, ...)


    → device needed Integration testing (interaction Activities - Services, camera app, ...)
  65. Build system

  66. Android project setup & build ant gradle

  67. Android build system

  68. Android Maven plugin https://code.google.com/p/maven-android-plugin/ <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.8.0</version> </plugin>!

  69. 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
  70. 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
  71. 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 ✔ ✔
  72. Mocks Easymock ! Mockito - uses DexMaker to generate mock

    classes
  73. 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.
  74. Mockito Prepare the mock + behavior ! Test the method

    of interest ! Validate that the mock saw what we expected / didn’t see anything unexpected
  75. Exercise

  76. 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/
  77. Exercise

  78. 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
  79. Android FEST assertEquals(View.GONE, view.getVisibility()); assertThat(view).isGone(); expected: <8> but was: <4>

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

    Expected visibility <gone> but was <invisible>. JUNIT ANDROID FEST
  81. 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);
  82. Exercise

  83. Spoon Automate test execution across multiple devices ! Aggregate the

    results ! Aggregation of screenshots http://square.github.io/spoon/
  84. Spoon: overview

  85. Spoon: device view

  86. Spoon: test view

  87. Spoon: screenshots Visual inspection of tests execution Spoon.screenshot(activity, "initial_state"); /*

    Normal test code... */ Spoon.screenshot(activity, "after_login");
  88. Exercise

  89. Behavior Driven Development

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

    ! !
  91. Third party libraries Robotium RoboElectric Mockito Fest Android Spoon

  92. Cloud testing

  93. A better Android emulator Automate testing ! Dozens of devices

    - different screen sizes - different Android versions
  94. Manymo

  95. Android Gradle integration

  96. CI

  97. None
  98. Continuous integration benefits Fast feedback - fewer errors Test everything

    on every (nightly) build Less manual testing Regression tests without additional effort
  99. Android

  100. None
  101. None
  102. Other

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

  104. Repository manager

  105. Architecture Simple architecture ! Dependency injection

  106. Code quality

  107. Measure...

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

  109. Quality Tools for Android

  110. 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
  111. Better Android apps

  112. References

  113. Suggested reading Android Application Testing Guide Diego Torres Milano (9781849513500)

    Robotium Automated Testing for Android 
 Hrushikesh Zadgaonkar (9781782168010) ! 

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

  115. Suggested viewing Google IO sessions

  116. Q + A

  117. Filip maelbrancke TWITTER: @fmaelbrancke EMAIL: filip@maelbrancke.net THANK YOU EMAIL: info@appfoundry.be

    consultant @ AppFoundry