Faster Builds on Android

Faster Builds on Android

I talk about the importance of faster builds and share tips to speed them up with Gradle and modularisation. Doing it right, you can change code in the business logic and run the tests for it in 2 seconds instead of 2 minutes.

D8796cfbfd245458dd193fbc4313d777?s=128

Philipp Hofmann

November 06, 2018
Tweet

Transcript

  1. Make diabetes suck less

  2. Testing Trophy

  3. Write tests. Not too many. Mostly integration. By Kent C.

    Dodds
  4. Faster Builds on Android From 2 Minutes to 2 Seconds

    @muffls Philipp Hofmann
  5. Why is build speed important?

  6. Scenario • 10 - 100 builds per day • Reduce

    build time by 118s • Save around 20 min to 2 hours per day • Save around 1,6 hours to 10 hours per week
  7. Scenario • Reduce by 5 seconds • 20 builds *

    5s * 10 devs * 5 working days = 1,4 hours
  8. What do you do when you have to wait 2

    minutes?
  9. Or just 20 seconds?

  10. None
  11. Loose Focus

  12. Multitasking is killing your brain.

  13. Multitasking lets your IQ drop significantly.

  14. Like skipping whole night sleep.

  15. Or smoking weed. Image Source

  16. Checking your email.

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

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

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

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

  21. D Multitasking Is Killing Your Brain CREDIT: Getty Images

  22. Slow builds = slow tests test automation is a pain

  23. • Basic tips to tweak your builds • Modularisation •

    Gradle Build Scans • 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 Agenda • Basic Android knowledge appreciated • Tips for beginners • Example for experts to maybe learn something new INFO
  24. Basic tips to tweak your build

  25. Update Gradle

  26. Update JVM

  27. A word about dependencies

  28. Don’t use dynamic versions, such as “2.+”

  29. Avoid unnecessary and unused dependencies.

  30. Consider copying a class from a third party library.

  31. Transient dependencies can add a lot of costs.

  32. Minimise the repository count.

  33. Minimise the repository count. repositories { google() maven { url

    '../localmaven' } maven { url 'https://maven.fabric.io/public' } mavenCentral() jcenter() maven { url "https://jitpack.io" } flatDir { dirs 'libs' } }
  34. Use incremental build kotlin.incremental=true

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

    since the last time it was run, Gradle will not run it again.
  36. Cache task outputs org.gradle.caching=true

  37. Use parallel builds org.gradle.parallel=true

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

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

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

  41. Use instant run

  42. Create a build variant for development

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

    "xxhdpi" versionNameSuffix "-dev" applicationIdSuffix '.dev' signingConfig signingConfigs.debug buildConfigField "long", "TIMESTAMP", "0L" } full { dimension "mode" } }
  44. Optimize your build speed https://developer.android.com/studio/build/optimize-your-build

  45. Modularisation

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

    down your build.
  47. Biggest benefit when modules can be built in parallel.

  48. None
  49. None
  50. None
  51. None
  52. None
  53. None
  54. Change Run

  55. None
  56. None
  57. 18 seconds when in layout

  58. 7 seconds when in Fragment

  59. Change Run

  60. None
  61. None
  62. 6 seconds when in layout

  63. 3 seconds when in a Fragment

  64. Change Run

  65. 65 seconds

  66. Change Run

  67. Change Run

  68. 65 seconds

  69. 9 seconds

  70. Change BL code and run tests

  71. first time ~30 seconds

  72. second time 10-15 seconds

  73. Change BL code and run tests

  74. Change BL code and run tests

  75. first time ~4 seconds

  76. second time 4 seconds

  77. I promised 2 seconds

  78. Plain Kotlin or Java No Android dependencies

  79. Demo of DefaultTapLevelCalculator

  80. Advantages of multiple modules

  81. Advantages of multiple modules • For big projects huge speed

    up, if done right. • Add smaller test app of a specific feature for very fast iterations. • Build and test them in parallel on CI. • Force developers to have a clear separation of concerns. • And all the other advantages … (Reusability, Maintainability, SOLID, …)
  82. Spotify has over 600 modules.

  83. 2015 2016 2017 2018 LOC in repo LOC NOT in

    the monolith LOC in the monolith
  84. :app :features :common :libs :http :logger :audio :video :artist :search

    :ui :library :feed :profile :radio :radio :android :ui-tools :follow :share :mobius :dep-inj
  85. Modularisation - How hard can it be? (Droidcon London 2018)

  86. Scan your Builds

  87. ./gradlew assembleDebug --scan

  88. ./gradlew assembleDebug --scan BUILD SUCCESSFUL in 6s Do you accept

    the Gradle Cloud Services license agreement (https:// gradle.com/terms-of-service)? [yes, no] yes Gradle Cloud Services license agreement accepted. Publishing build scan... https://gradle.com/s/czajmbyg73t62
  89. None
  90. First build time

  91. Enable Parallel Builds

  92. Change one line in business logic

  93. Splitting up into multiple modules

  94. Don’t just blindly optimise

  95. Scan Optimise

  96. Conclusion

  97. Build speed is important Image Source

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

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

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

    down your build.
  101. I optimize the build speed!

  102. Don’t just blindly optimise

  103. Scan Optimise

  104. Slides on Speakerdeck tomorrow. Check my twitter account @muffls

  105. Feedback bit.ly/phil-feedback

  106. Questions ?