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

Continuous Integration for Android Applications

Continuous Integration for Android Applications

Droidcon NYC 2015

My introduction to CI for Android Applications complete with an overview of why you should use CI, a comparison of contemporary CI tools, how to set up your own Jenkins instance and get builds flowing, and Jenkins tips and tricks

Stephen D'Amico

August 28, 2015
Tweet

More Decks by Stephen D'Amico

Other Decks in Programming

Transcript

  1. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Continuous Integration for Android Applications 1
  2. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 What is Continuous Integration? (and what can it do for me?) 2
  3. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Continuous Integration • Refers to a suite of tools that enable: • Automated building and deployment of applications • Automated testing of code changes • Code health reports (test pass %, code coverage) • Build artifact storage 3
  4. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Why would we want to use Continuous Integration (CI) on Android? 4
  5. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 – Someone Probably This “Good developers are lazy” 5
  6. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 The manual release process 1. Ensure version control is on latest commit 2. Open IDE -> Build -> Generate Signed APK 3. Select Keystore and enter usernames/passwords 4. Select destination path, build type and flavor 5. Navigate to Play Store Developer console 6
  7. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Manual release (cont.) 6. Open application on Dev console and navigate to APK upload 7. Browse to APK on filesystem 8. Upload… 9. Update release notes (try to be clever?) 10. Publish 7
  8. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Manual release (cont.) • What about tagging the release in version control? pushing tags? branch cleanup? • Saving the APK to Dropbox/Github? • Emailing the APK to QA? Teammates? • Did you run your tests? • What if you forget any of these steps? 8
  9. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Why would we want to use CI on Android? Besides being a good, efficient developer who doesn’t want to manually do everything 9
  10. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 CI for Android • Assemble APK’s/AAR’s • Deploy to Play Store/maven/s3/storage • Create version control tags, merge branches, and more • Run unit/UI tests • With a little elbow grease: on-device instrumentation tests 10
  11. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Kevin Schultz told us to • Last year, Kevin Schultz (Gilt Group) presented on Building Maintainable Android Applications • https://goo.gl/2Ar4jh 11
  12. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 What did Kevin tell us? • Need to build a maintainable ecosystem around our Application • Unit tests, functional tests, UI tests, build, and CI 12
  13. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 What did Kevin tell us? • Need to build a maintainable ecosystem around our Application • Unit tests, functional tests, UI tests, build, and CI 13
  14. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 I want CI, what are my options? 14
  15. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Write it yourself! • git hooks on your self-hosted git server • Trigger various builds based on branch names/tags • curl APK’s to API’s • cron for nightly builds 15
  16. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Don’t reinvent the wheel • Common/popular options for CI tools: • Jenkins (OSS), Travis CI (OSS, Hosted), Circle CI (Hosted), TeamCity (JetBrains), Bamboo (Atlassian) • But there are many more! 16
  17. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Popular CI Tools compared 17 Jenkins Travis CI Circle CI Bamboo TeamCity Creator OSS Travis CI Circle CI Atlassian JetBrains OSS Yes Yes No No No Free To Use Yes Yes (for oss, otherwise 1 builder) Yes (for 1 build container) No Yes (up to 3 build agents) Requires Hosting Yes No No No Yes
  18. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Jenkins • Free and Open Source • Highly configurable (but also complex) • Can deploy yourself or choose a cloud provider • Written in Java (contribute back!) • Vast selection of plugins for accomplishing various tasks 18
  19. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Deployment • Local Deployment • Advantages • Can be a Mac mini/pro (OSX) • Can connect test devices to your build box • Disadvantages • Manage own hardware • Manage own backups 19 • Cloud Deployment • Advantages • Don’t have to manage own hardware • Docker image available • Disadvantages • Slightly more challenging to access a local device lab • OSX?
  20. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 CloudBees • Cloud hosted Jenkins platform • Founder originated the Jenkins project • Various paid tiers available 20
  21. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 CloudBees (cont.) • Advantages • Don’t have to manage hardware • Don’t have to manage deployment • Don’t have to manage Android SDK versions 21 • Disadvantages • Generally beholden to Android SDK currently installed on platform (can work around) • Same with JDK versions • General cloud issue: cannot connect to local physical devices
  22. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 So which should I pick? • Balance the amount of effort settings up vs time saved • Small team (1-3): local machine or CloudBees • Medium team (3-6): local machine, unless operations can manage for you • Large team (6+): CloudBees or self-deployed cloud 22
  23. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Setting up our Mac • Install homebrew • Create a jenkins user • Then, brew install git android jenkins • Run jenkins • Navigate to http://localhost:8080 23
  24. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 First things first • Get in, and get familiar 24
  25. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 First things first • Let’s get some plugins! 25
  26. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Plugins you must install • Gradle • For executing gradle scripts during build • Git SCM • For interfacing with git repos 26
  27. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Environment configuration • Jenkins -> Manage -> Configure System • Check Env Vars box • Add our $ANDROID_HOME 27
  28. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Adding some credentials • In case you need to git over ssh • Or you want to store some http credentials • Useful to set up an email on your domain for jenkins so that he can have read-only access to source control, etc. 28
  29. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Create a job • From the main screen, “New Item” on the left • Name it appropriately 29
  30. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Configuring the build • Set your build description and build name if you’d like • Scroll to SCM • Select git • Add your repository url • Select appropriate credentials (if needed) 30
  31. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Configuring the build (cont.) • Select what branches should build when this job is triggered • Can have separate jobs for separate branches • e.g. Beta APK builder for branches that start with release-** 31
  32. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Configuring the build (cont.) • Many different actions can be taken during the build process • Plugins can add steps to the menu of options • For Android, you may want to execute a few different gradle tasks 32
  33. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Configuring the build • Many different actions can be taken during the build process • Plugins can add steps to the menu of options • For Android, you may want to execute a few different gradle tasks 33
  34. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Running the build • Save you configuration and then “Build Now” • Should trigger a build • Open the build view to examine the console output 34
  35. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Running the build • Watch console output whiz on by, whoo! • Useful to print out env vars and other debugging information • Every command executed is echo’d to console by default 35
  36. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Post build • Can view our workspace to see outputs 36
  37. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Great, now I have builds… what’s next? Making a house a home! 37 LET ME EXPLAIN YOU BUILD CUSTOMIZATION
  38. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Github PR Builder plugin • Build your PR’s when they’re submitted • Update github checks API • Run tests • Get health reports 38
  39. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Google Play Publisher plugin • After build completes, upload APK • Can also publish to Beta or promote APKs 39
  40. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Amazon EC2 plugin • Take advantage of Amazon’s cloud to scale-up • Maybe that Mac mini isn’t powerful enough • Easily start build slaves in the cloud 40
  41. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 HockeyApp/S3 upload plugins • Available as plugins • Easy saving build artifacts • Easily distribute builds to teammates/QA/whomever 41
  42. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Other plugins of interest • Android Emulator • Android Lint • JUnit • Slack/Hipchat/IRC 42
  43. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Keystore • Install them on the jenkins user’s home dir • Reference them from gradle • Easy enough to to an .exists() check to fallback • Store username and password in a .properties file so its not in gradle and vcs • No more emailing keys 43
  44. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Gradle • Jenkins just executes your grade scripts • Gradle scripts are written in groovy • Anything you can write in groovy, you can have Jenkins do 44
  45. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Gradle performance • Disable pre-dex for CI builds • dexOptions { preDexLibraries = !isJenkinsBuild() } • Don’t use daemon • Only build what you need to 45
  46. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Useful things to remember • When in doubt, log it • Use Jenkins’ Script Console • Jenkins also has server logs 46
  47. CI for Android Apps - Stephen D’Amico - Droidcon NYC

    2015 Thank you! • Resources and more: • Kevin’s talk on Maintainable Android Apps: https://goo.gl/2Ar4jh • http://jenkins-ci.org/ • Twitter: @sddamico • Github: sddamico 47