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/

AppFoundry

June 13, 2014
Tweet

More Decks by AppFoundry

Other Decks in Programming

Transcript

  1. Build pipeline Checkout / compile Unit tests Test coverage Code

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

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

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

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

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

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

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

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

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

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

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

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

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

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


    → device needed Integration testing (interaction Activities - Services, camera app, ...)
  23. 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
  24. 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
  25. 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 ✔ ✔
  26. 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.
  27. Mockito Prepare the mock + behavior ! Test the method

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

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

    - different screen sizes - different Android versions
  33. CI

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

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

    Robotium Automated Testing for Android 
 Hrushikesh Zadgaonkar (9781782168010) ! 

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