Faster builds on Android - from 2 minutes to 2 seconds

Faster builds on Android - from 2 minutes to 2 seconds

What does a developer do when a build takes 2 minutes? He gets bored and his mind starts to wander. Maybe he opens Reddit, Twitter orMedium or checks his emails. Switching context and multitasking kills productivity. Every developer needs fast feedback, so he can jump into the flow state. Therefore, fast builds are essential for productive development.

First, we take a look into Gradle Build Scans. There is no sense in accelerating your build without measuring and comparing the different outcomes. Gradle Build Scans help you to understand what happens during your build and which parts consume the most time.

Then we continue with basic tips to tweak your build with Gradle. Most of those tips are quick wins, because they only require a simple change in the gradle build settings.

Finally, we take a look into how modularisation influences the build speed through an example. By doing it right you can change your business logic and run your tests in 2 seconds instead of 2 minutes.

D8796cfbfd245458dd193fbc4313d777?s=128

Philipp Hofmann

April 05, 2019
Tweet

Transcript

  1. Faster Builds on Android From 2 Minutes to 2 Seconds

    @muffls Philipp Hofmann
  2. @muffls Philipp Hofmann

  3. Make diabetes suck less

  4. Why is build speed important? Photo by Chris Liverani on

    Unsplash
  5. How often do you hit compile?

  6. 10 - 100 times per day?

  7. From 2 minutes to 2 seconds.

  8. We save 118 seconds.

  9. Around 20 minutes to 2 hours per day.

  10. Around 1,6 to 10 hours per week.

  11. Reduce by 5 seconds.

  12. 20 builds * 5s * 10 devs * 5 days

    = 1,4 hours / week
  13. 100 builds * 5s * 10 devs * 5 days

    = 9,6 hours / week
  14. What do you do when you have to wait 2

    minutes? Photo by NordWood Themes on Unsplash
  15. Or just 20 seconds? Photo by NordWood Themes on Unsplash

  16. None
  17. Photo by Stefan Cosma on Unsplash

  18. Multitasking is killing your brain.

  19. Multitasking lets your IQ drop significantly.

  20. Like skipping whole night sleep.

  21. Or smoking weed. Image Source

  22. Knowing there is an unread email can reduce your IQ

    by 10 points!
  23. Multitasking is especially bad for men.

  24. It can reduce the IQ up to 15 points.

  25. Turning you into a cognitive equivalent of an 8-year-old.

  26. Multitasking Is Killing Your Brain CREDIT: Getty Images

  27. Slow Builds = Slow Tests Test automation is a pain.

  28. Faster Builds

  29. Faster Builds = More Frequent Feedback

  30. Faster Builds = More Frequent Feedback = Faster Development

  31. • Measure Your Builds • Quick Wins to Fasten Your

    Builds • Modularisation and Build Speed • Some additional information about the topic on the chart • Maybe some numbers or a background information about some insights • Here is just another placeholder text to visualise the look of a filled info box INFO BOX Topics
  32. Measure Your Builds Photo by Annie Spratt on Unsplash

  33. $ ./gradlew clean cleanBuildCache

  34. $ ./gradlew assembleDebug --scan

  35. $ ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 1s 84 actionable

    tasks: 84 up-to-date Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https:// gradle.com/terms-of-service. Do you accept these terms? [yes, no]
  36. $ ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 1s 84 actionable

    tasks: 84 up-to-date Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https:// gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes
  37. $ ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 1s 84 actionable

    tasks: 84 up-to-date Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https:// gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes Gradle Terms of Service accepted.
  38. $ ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 1s 84 actionable

    tasks: 84 up-to-date Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https:// gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes Gradle Terms of Service accepted. Publishing build scan...
  39. $ ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 1s 84 actionable

    tasks: 84 up-to-date Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https:// gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes Gradle Terms of Service accepted. Publishing build scan... https://gradle.com/s/glklref6wdxr6
  40. None
  41. None
  42. None
  43. None
  44. Create Build Scans https://guides.gradle.org/creating-build-scans/

  45. Profile Your Build https://developer.android.com/studio/build/optimize-your-build#profile

  46. $ ./gradlew assembleDebug --profile

  47. None
  48. None
  49. Measure Optimise

  50. Quick Wins to Fasten Your Builds Photo by Richard R

    Schünemann on Unsplash
  51. Update your Tools Photo by Todd Quackenbush on Unsplash

  52. Update Gradle

  53. None
  54. None
  55. Improving build speed in Android Studio https://medium.com/androiddevelopers/improving-build-speed-in-android- studio-3e1425274837

  56. Update JVM

  57. Update Android Studio and SDK Tools

  58. Update Android Plugin for Gradle

  59. • Gradle • JVM • Android Studio and SDK Tools

    • Android Plugin for Gradle Update
  60. Use instant run

  61. Deploy to a device with Android API level 24 or

    higher.
  62. gradle.properties

  63. Caching org.gradle.caching=true

  64. If neither a task’s inputs nor its output have changed

    since the last time it was run, Gradle will not run it again.
  65. Enabled by default. Since Android Plugin 2.3.0 https://developer.android.com/studio/build/build-cache

  66. None
  67. Gradle Build Cache https://docs.gradle.org/current/userguide/build_cache.html

  68. Incremental Compilation kotlin.incremental=true

  69. Parallel Builds org.gradle.parallel=true

  70. Create a build variant for development

  71. flavorDimensions "mode" productFlavors { dev { dimension "mode" resConfigs "en",

    "xxhdpi" buildConfigField "long", "TIMESTAMP", "0L" } full { dimension "mode" } }
  72. Quick wins 1. Update your Tools 2. Use Instant Run

    3. Caching 4. Incremental Compilation 5. Parallel Builds 6. Build Variant for Develop Photo by Richard R Schünemann on Unsplash
  73. Professional Stuff

  74. Optimize your build speed https://developer.android.com/studio/build/optimize-your-build

  75. Improving the Performance of Gradle Builds https://guides.gradle.org/performance/

  76. Check your memory settings org.gradle.jvmargs= -Xmx3g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

  77. Java Performance Tuning https://dzone.com/articles/java-performance-tuning

  78. Modularisation and Build Speed

  79. Modules can speed up your build, but can also slow

    down your build.
  80. 1. Build modules in parallel

  81. None
  82. None
  83. None
  84. None
  85. Example

  86. Small app with one module.

  87. None
  88. None
  89. None
  90. Optimise • Update gradle and gradle plugin • Update Kotlin

    • Enable parallel builds and incremental compilation • Split up code into multiple modules
  91. None
  92. Reduced build time for around 25%.

  93. 2. Build just what you need

  94. None
  95. None
  96. A taste of bluetooth.

  97. Change Run

  98. None
  99. None
  100. 18 seconds when in layout

  101. 7 seconds when in Fragment

  102. Change Run

  103. None
  104. None
  105. 6 seconds when in layout

  106. 3 seconds when in Fragment

  107. Change Run

  108. 65 seconds

  109. Change Run

  110. Change Run

  111. 65 seconds

  112. 9 seconds

  113. Build just what you need • Build only the code

    you want to check. • Let you iterate faster on your feature modules.
  114. 3. Faster Tests

  115. Run tests 15 seconds

  116. Run tests 4 seconds

  117. I promised 2 seconds.

  118. Plain Kotlin or Java No Android dependencies

  119. None
  120. Advantages of Multiple Modules

  121. Advantages of Multiple Modules 1. Build modules in parallel 2.

    Build just what you need 3. Faster Tests 4. And all the other advantages … (Reusability, Maintainability, SOLID, …)
  122. Spotify has over 600 modules.

  123. Modularisation - How hard can it be? (Droidcon London 2018)

  124. Sum it up

  125. Faster Builds = Faster Development Photo by Andrea Leopardi on

    Unsplash
  126. Photo by Richard R Schünemann on Unsplash Quick wins 1.

    Update your tools 2. Use instant run 3. Use Gradle caching 4. Incremental compilation 5. Parallel builds 6. Build variant for develop
  127. Modules can speed up your build, but can also slow

    down your build.
  128. Advantages of multiple modules 1. Build modules in parallel 2.

    Build just what you need 3. Faster Tests
  129. I optimise the build speed!

  130. Measure Optimise

  131. @muffls Philipp Hofmann

  132. @muffls Philipp Hofmann Thanks!

  133. Questions ? http://bit.ly/droidconit-faster-builds-feedback @muffls Philipp Hofmann