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

Android Application Development: Better, Faster, Stronger! at Devoxx 2013

Android Application Development: Better, Faster, Stronger! at Devoxx 2013

Android Application Development hands-on lab @ Devoxx 2013

Filip Maelbrancke

November 11, 2013
Tweet

More Decks by Filip Maelbrancke

Other Decks in Programming

Transcript

  1. Your hosts Filip Maelbrancke (@fmaelbrancke) Peter Kuterna (@peterkuterna) ! !

    Consultants at iDA MediaFoundry (@iDAMediaFoundry) / XTi
  2. Build pipeline Checkout / compile Unit tests Test coverage Code

    analysis Create deployable artifact Deploy for automatic QA test Trigger automated QA stage
  3. Gradle? why - goals Powerful build system declarative, flexible tooling

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

    ! dependencies { ! classpath ‘com.android.tools.build:gradle:0.6’ ! }! }! ! apply plugin: ‘android’! ! android { ! compileSdkVersion 19 ! buildToolsVersion ‘19.0.0’! }! !
  5. Build types build / packaging customization debuggable flag ProGuard signing

    configuration source / resources overlay debug and release prebuilt
  6. remote dependencies repositories { mavenCentral() } android { ... }

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

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

    for - assets - ProGuard rules - Custom Lint Rules - ...
  9. Testing Manual testing is tedious ! Humans are not very

    good at testing ! But computers are...
  10. “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
  11. 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
  12. 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
  13. 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
  14. (Unit) testing in Android Android SDK (JUnit 3-based) Instrumentation tests

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

    MyTestClass extends TestCase { public void testMethodName() throws Exception { } }
  16. 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
  17. Android instrumentation tests Isolate tests from the system: MockApplication MockContext

    MockResources MockContentProvider MockContentResolver MockPackageManager
  18. The monkey Generates pseudo-random stream of user events: clicks touches

    gestures system-level events ! stress test your app
  19. 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
  20. Robolectric AndroidTestCase needs an instance of the emulator to run

    ! Big time sink: - spin up emulator - deploy the APK - run the actual tests
  21. 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)
  22. Robolectric advantages Unit testing fast and easy Make your own

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


    → device needed Integration testing (interaction Activities - Services, camera app, ...)
  24. 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
  25. 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
  26. Gradle vs Maven Gradle Maven Build variants / product flavors

    ✔ ✖ Android test framework ✔ ✔ Robotium ✔ ✔ Robolectric ✔ ✔ UIAutomator / Monkey ✖ ✔ Code coverage ✖ ✔ Code quality (pmd, checkstyle, findbugs, lint) ✔ ✔ Aggregation ✔ ✔ Source: https://github.com/stephanenicolas/Quality-Tools-for-Android
  27. 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.
  28. Mockito Prepare the mock + behavior ! Test the method

    of interest ! Validate that the mock saw what we expected / didn’t see anything unexpected
  29. 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/
  30. 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
  31. 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);
  32. Spoon Automate test execution across multiple devices ! Aggregate the

    results ! Aggregation of screenshots http://square.github.io/spoon/
  33. A better Android emulator Automate testing ! Dozens of devices

    - different screen sizes - different Android versions
  34. CI

  35. Continuous integration benefits Fast feedback - fewer errors Test everything

    on every (nightly) build Less manual testing Regression tests without additional effort
  36. 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
  37. Suggested reading Android Application Testing Guide Diego Torres Milano (9781849513500)

    Robotium Automated Testing for Android 
 Hrushikesh Zadgaonkar (9781782168010) ! 

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