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 get 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 by an example. 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

September 03, 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. Gradle Profiler https://github.com/gradle/gradle-profiler

  50. Talaiot https://github.com/cdsap/Talaiot

  51. None
  52. Measure Optimise

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

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

  55. Update Gradle

  56. None
  57. None
  58. Improving build speed in Android Studio https://medium.com/androiddevelopers/improving-build-speed-in-android- studio-3e1425274837

  59. Update JVM

  60. Update Android Studio and SDK Tools

  61. Update Android Plugin for Gradle

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

    • Android Plugin for Gradle Update
  63. Use Apply Changes

  64. For devices and emulators running on Android 8.0 or newer.

  65. gradle.properties

  66. Caching org.gradle.caching=true

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

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

  69. None
  70. Gradle Build Cache https://docs.gradle.org/current/userguide/build_cache.html

  71. Incremental Compilation kotlin.incremental=true

  72. Incremental annotation processor kapt.incremental.apt=true

  73. Parallel Builds org.gradle.parallel=true

  74. Create a build variant for development

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

    "xxhdpi" buildConfigField "long", "TIMESTAMP", "0L" } full { dimension "mode" } }
  76. Quick wins 1. Update your Tools 2. Use Apply Changes

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

  78. Scope annotation processing

  79. Apply only needed plugins

  80. apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply

    plugin: 'maven-publish' apply plugin: 'kotlin-kapt' apply plugin: 'talaiot' apply plugin: 'io.gitlab.arturbosch.detekt' apply plugin: 'org.jlleitschuh.gradle.ktlint'
  81. Optimize your build speed https://developer.android.com/studio/build/optimize-your-build

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

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

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

  85. 19 tips for Gradle in Android projects — 2019 Edition

    https://medium.com/google-developer-experts/19-tips-for-gradle-in-android- projects-2019-edition-11af704eb06e
  86. Modularisation and Build Speed

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

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

  89. None
  90. None
  91. None
  92. None
  93. Example

  94. Small app with one module.

  95. None
  96. None
  97. None
  98. Optimise • Update gradle and gradle plugin • Update Kotlin

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

  101. 2. Compilation avoidance

  102. None
  103. None
  104. A taste of bluetooth.

  105. Change Run

  106. None
  107. None
  108. 18 seconds when in layout

  109. 7 seconds when in Fragment

  110. Change Run

  111. None
  112. None
  113. 6 seconds when in layout

  114. 3 seconds when in Fragment

  115. Change Run

  116. 65 seconds

  117. Change Run

  118. Change Run

  119. 65 seconds

  120. 9 seconds

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

    you want to check. • Let you iterate faster on your feature modules.
  122. api implementation vs

  123. api implementation Module A Module B

  124. Module A Module B Module C Foo api

  125. Module A Module B Module C Foo api

  126. Module A Module B Module C Foo api

  127. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api
  128. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api
  129. Module A Module B Module C implementation Foo Module A

    Module B Module C Foo api
  130. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api
  131. Module A Module B Module C implementation Foo Module A

    Module B Module C Foo api
  132. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api ModuleB is part of my Public API ModuleB is my Implementation Detail
  133. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api ModuleB is part of my Public API ModuleB is my Implementation Detail
  134. Module A Module B Module C Foo implementation Module A

    Module B Module C Foo api ModuleB is part of my Public API ModuleB is my Implementation Detail
  135. @FMuntenescu Florina Muntenescu

  136. 3. Faster Tests

  137. Run tests 15 seconds

  138. Run tests 4 seconds

  139. I promised 2 seconds.

  140. Plain Kotlin or Java No Android dependencies

  141. None
  142. https://medium.com/google-developer-experts/19-tips-for-gradle-in-android-projects-2019-edition-11af704eb06e :libjava:assemble of the base java plugin

  143. https://medium.com/google-developer-experts/19-tips-for-gradle-in-android-projects-2019-edition-11af704eb06e :lib1:assembleDebug of the com.android.library

  144. Create pure Java Kotlin / Libraries

  145. Advantages of Multiple Modules

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

    Compilation avoidance 3. Faster tests 4. And all the other advantages … (Reusability, Maintainability, SOLID, …)
  147. Spotify has over 600 modules.

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

  149. Sum it up

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

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

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

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

    Compilation avoidance 3. Faster tests
  154. Create pure Java Kotlin / Libraries

  155. I optimise the build speed!

  156. Measure Optimise

  157. @muffls Philipp Hofmann

  158. Droidcon Turin 2019

  159. @muffls Philipp Hofmann Questions?