Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android - Gradle build optimisation

3d83f31339d239abcc55f869e5f30348?s=47 bkosarzycki
July 26, 2018
73

Android - Gradle build optimisation

Simple tips on how to speed up your Android builds.
This talk is inspired by Stefan Oehme ( Gradle, Lead Developer) talk on “IMPROVING ANDROID BUILD PERFORMANCE” - Droidcon Berlin 2018

3d83f31339d239abcc55f869e5f30348?s=128

bkosarzycki

July 26, 2018
Tweet

Transcript

  1. Gradle optimisation Speed up kotlin/android builds Bartosz Kosarzycki @bkosarzycki July

    26, 2018
  2. Gradle build phases: - Initialization - Configuration - Execution

  3. +

  4. How can we solve this? - Modularization => Compile avoidance

    - Decoupled code => Faster incremental builds
  5. Increase memory which is available to Android Studio

  6. Increase memory Default: 1246 MB

  7. Check gradle wrapper version

  8. Currently the newest version is: 4.8.1 Check gradle version {project_home}/gradle/wrapper/gradle-wrapper.properties

  9. - In 4.8.1 disable Google’s experimental “Configure on demand” *

    What went wrong: A problem occurred evaluating project ':repository'. > Failed to apply plugin [id 'com.android.library'] > Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.configureondemand=false in your gradle.properties file or use a Gradle version less than 4.6. Check gradle version - “instant run” is lost due to Android Studio & gradle incompatibility
  10. Do not mix Java/Kotlin files in one module

  11. Solution: Extract legacy Java files into a separate module if

    you cannot convert them to kotlin
  12. Enable build caches

  13. Solution: org.gradle.caching=true Info: https://developer.android.com/studio/build/build-cache Clear cache: ./gradlew cleanBuildCache {project_home}/gradle.properties

  14. Prevent Crashlytics from ruining your incremental builds

  15. Fix for Crashlytics buildTypes { debug { debuggable true minifyEnabled

    false shrinkResources false ext.enableCrashlytics = false ext.alwaysUpdateBuildId = false } }
  16. Use offline mode when no new dependencies are added

  17. Disable multidex

  18. Disable multidex android { defaultConfig { multiDexEnabled false } }

    // MIN API LEVEL : 21
  19. None
  20. Predex libraries

  21. Predex libraries android { dexOptions { preDexLibraries true } }

    Predexing builds dex file out of libraries so it can be used in incremental builds (not building dex files each time for libraries). Using this feature increases clean build time.
  22. Avoid annotation processors especially those breaking incremental builds

  23. Solution: - Use gradle > 4.7 - Use only Annotation

    Processor which do support incremental builds - Google’s data-binding AP does NOT support it (work in progress) https://github.com/gradle/gradle/issues/5277 Incremental Annotation Processors:
  24. Keep kotlin lang & libraries up-to-date

  25. Enable D8 Next generation dex compiler

  26. None
  27. Android R8 compiler is still experimental (July 2018)

  28. Source: Droidcon NYC 2016 - Lightning fast Android builds with

    Gradle + Buck
  29. BAZEL builds (Google): $> brew install bazel - different project

    structure - create WORKSPACE file with sdk info - create BUILD file with
  30. None
  31. Use gradle profiler

  32. - download repository here + docs - compile - make

    an alias for ‘gradle-profiler’ Install gradle profiler: > ./gradlew installDist cp ./build/install/gradle-profiler/* ~/custom/gradle-profiler alias gradle-profiler='~/custom/gradle-profiler/bin/gradle-profiler'
  33. Profile build performance: > ./gradlew clean > gradle-profiler --benchmark --project-dir

    . assembleDebug > open ./profile-out/benchmark.html - from inside project’s directory:
  34. None
  35. None
  36. Gradle profiler with FlameGraphs

  37. - Linux-only (currently) - Download ‘honest-profiler’ & ‘flame-graph’ Gradle-profiler with

    FlameGraph > gradle-profiler --profile hp --hp-home ~/custom/honest-profiler --fg-home ~/custom/flame-graph --project-dir . assembleDebug
  38. Use gradle scans http://scans.gradle.com

  39. Gradle scans: $ ./gradlew build --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 - uses cloud web services
  40. None
  41. None
  42. None
  43. Future: use static/binary/ prebuilt libraries ?

  44. Android Studio Preferences

  45. Android Studio Preferences

  46. Convert Android Traceview into flamegraphs

  47. Convert Android Traceview...

  48. Into Flamegraph LINK

  49. Links / References - Stefan Oehme / Gradle, Lead Developer

    - Droidcon Berlin 2018 - “IMPROVING ANDROID BUILD PERFORMANCE” / Intermediate / Best Practices / Code Quality / Talk - link - Speed-up Android builds - link - TraceView into FlameGraph - link - Gradle-Profiler - link
  50. QUESTIONS?