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 09, 2019
Tweet

Transcript

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

    @muffls Philipp Hofmann
  2. Why is build speed important? Photo by Chris Liverani on

    Unsplash
  3. How often do you hit compile?

  4. 10 - 100 times per day?

  5. From 2 minutes to 2 seconds.

  6. We save 118 seconds.

  7. Around 20 minutes to 2 hours per day.

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

  9. Reduce by 5 seconds.

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

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

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

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

  14. None
  15. Photo by Stefan Cosma on Unsplash

  16. Multitasking is killing your brain.

  17. Multitasking lets your IQ drop significantly.

  18. Like skipping whole night sleep.

  19. Or smoking weed. Image Source

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

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

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

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

  24. Multitasking Is Killing Your Brain CREDIT: Getty Images

  25. Slow Builds Test automation is a pain. Slow Tests

  26. Faster Builds

  27. More Frequent Feedback Faster Builds

  28. Faster Development More Frequent Feedback Faster Builds

  29. Faster Development More Frequent Feedback Faster Builds

  30. Faster Development More Frequent Feedback Faster Builds

  31. Faster Development More Frequent Feedback Faster Builds

  32. Faster Development More Frequent Feedback Faster Builds

  33. Faster Development More Frequent Feedback Faster Builds Faster Builds Pyramid

  34. • 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
  35. Measure Your Builds Photo by Annie Spratt on Unsplash

  36. $ ./gradlew assembleDebug --scan

  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]
  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
  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.
  40. $ ./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...
  41. $ ./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
  42. None
  43. None
  44. None
  45. None
  46. Create Build Scans https://guides.gradle.org/creating-build-scans/

  47. $ ./gradlew assembleDebug --profile

  48. None
  49. None
  50. Profile Your Build https://developer.android.com/studio/build/optimize-your-build#profile

  51. Gradle Profiler https://github.com/gradle/gradle-profiler

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

  53. None
  54. $ ./gradlew clean cleanBuildCache

  55. Measure Optimise

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

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

  58. Update Gradle

  59. None
  60. None
  61. Improving build speed in Android Studio https://medium.com/androiddevelopers/improving-build-speed-in-android- studio-3e1425274837

  62. Update JVM

  63. Update Android Studio and SDK Tools

  64. Update Android Plugin for Gradle

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

    • Android Plugin for Gradle Update
  66. Use Apply Changes

  67. gradle.properties

  68. Caching org.gradle.caching=true

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

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

  71. None
  72. Gradle Build Cache https://docs.gradle.org/current/userguide/build_cache.html

  73. Incremental Compilation kotlin.incremental=true

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

  75. Parallel Builds org.gradle.parallel=true

  76. Create a build variant for development

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

    "xxhdpi" buildConfigField "long", "TIMESTAMP", "0L" } full { dimension "mode" } }
  78. 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
  79. More Stuff

  80. Scope annotation processing

  81. Apply only needed plugins

  82. 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'
  83. Improving the Performance of Gradle Builds https://guides.gradle.org/performance/

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

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

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

  87. 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
  88. Modularisation

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

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

  91. None
  92. None
  93. None
  94. None
  95. Example

  96. Small app with one module.

  97. None
  98. None
  99. None
  100. Optimise • Update gradle and gradle plugin • Update Kotlin

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

  103. 2. Compilation avoidance

  104. None
  105. None
  106. api implementation vs

  107. api implementation Module A Module B

  108. Module A Module B Module C Foo api

  109. Module A Module B Module C Foo api

  110. Module A Module B Module C Foo api

  111. Module A Module B Module C Foo implementation Module A

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

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

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

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

    Module B Module C Foo api
  116. 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
  117. 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
  118. 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
  119. @FMuntenescu Florina Muntenescu

  120. Change

  121. None
  122. None
  123. 18 seconds when in layout

  124. 7 seconds when in Fragment

  125. Change

  126. None
  127. None
  128. 18 seconds when in layout

  129. 6 seconds when in layout

  130. 6 seconds when in layout 3 times faster

  131. 7 seconds when in Fragment

  132. 3 seconds when in Fragment

  133. 3 seconds when in Fragment 2,3 times faster

  134. Change Run

  135. 65 seconds

  136. Change Run

  137. Change Run

  138. 65 seconds

  139. 9 seconds

  140. 9 seconds 7 times faster

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

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

  143. Run tests 15 seconds

  144. Run tests 4 seconds

  145. I promised 2 seconds.

  146. Plain Kotlin or Java No Android dependencies

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

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

  150. Create pure Java Kotlin / Libraries

  151. Advantages of Multiple Modules

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

    Compilation avoidance 3. Faster tests 4. And all the other advantages … (Reusability, Maintainability, SOLID, …)
  153. Sum it up

  154. 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
  155. Create pure Java Kotlin / Libraries

  156. Advantages of multiple modules 1. Build modules in parallel 2.

    Compilation avoidance 3. Faster tests
  157. I optimise the build speed!

  158. Measure Optimise

  159. Faster Builds

  160. More Frequent Feedback Faster Builds

  161. Faster Development More Frequent Feedback Faster Builds

  162. @muffls Philipp Hofmann Faster Development More Frequent Feedback Faster Builds

  163. @muffls Philipp Hofmann Faster Development More Frequent Feedback Faster Builds