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
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
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
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
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
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
2015 Kevin Schultz told us to • Last year, Kevin Schultz (Gilt Group) presented on Building Maintainable Android Applications • https://goo.gl/2Ar4jh 11
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
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
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
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
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
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
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?
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
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
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
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
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
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
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
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
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
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
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
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
2015 Gradle performance • Disable pre-dex for CI builds • dexOptions { preDexLibraries = !isJenkinsBuild() } • Don’t use daemon • Only build what you need to 45