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

Improving Gradle Builds

Mohit S
January 25, 2022

Improving Gradle Builds

As the number of modules in your code base increases, speed and consistency of your Gradle builds becomes very important. However, improving your Gradle builds can be a tricky endeavor. In this talk, I’ll address these common questions in improving Gradle builds.

- How do we benchmark & profile our builds?
- How do you read build scans to identify problems?
- How do we maintain a consistent modular structure?
- What tools can you use to speed up builds?

I’ll take a deep dive into these questions with examples and insights from my experiences.

Mohit S

January 25, 2022
Tweet

More Decks by Mohit S

Other Decks in Programming

Transcript

  1. Uber Build Eval • Similar to Uber Production mobile apps

    • 1292 Modules • 21,235 Build Graph Edges
  2. How do we analyze our builds? • Build Analyzer in

    Android Studio • Gradle Enterprise
  3. Build Tags gradleEnterprise { buildScan { if (System.getenv("CI")) { tag

    "CI" } else { tag "Local" } tag System.getProperty("os.name") } }
  4. Build Tags gradleEnterprise { buildScan { if (System.getenv("CI")) { tag

    "CI" } else { tag "Local" } tag System.getProperty("os.name") } }
  5. Build Tags gradleEnterprise { buildScan { if (System.getenv("CI")) { tag

    "CI" } else { tag "Local" } tag System.getProperty("os.name") } }
  6. Build Tags gradleEnterprise { buildScan { if (System.getenv("CI")) { tag

    "CI" } else { tag "Local" } tag System.getProperty("os.name") } }
  7. Dependency Analysis Plugin > Task :LibraryB:projectHealth Unused dependencies which should

    be removed: implementation("androidx.appcompat:appcompat:1.4.1") implementation("androidx.core:core-ktx:1.7.0") implementation("com.google.android.material:material:1.5.0")
  8. Disable Remote Caching doctor { /** * Print a warning

    to the console if we spend more than this * amount of time with Dagger annotation processors. */ daggerThreshold = 5000 }
  9. Android Build Cache Plugin • Plugin by Gradle • Gradle

    plugin to fix Android build problems • Workarounds for Room
  10. Module Graph Assert Plugin Max height the number of edges

    on the longest path from the node to a leaf.
  11. Module Graph Assert Plugin > Task :app:assertMaxHeight FAILED 
 Module

    :app is allowed to have maximum height of 3, but has 4.
  12. Module Graph Assert Plugin moduleGraphAssert { maxHeight = 3 allowed

    = [‘:app’ -> ‘:feature-[a-z]’, ‘:feature.* -> :lib’] }
  13. Module Graph Assert Plugin moduleGraphAssert { maxHeight = 3 allowed

    = [‘:app’ -> ‘:feature-[a-z]’, ‘:feature.* -> :lib’] restricted = [‘:feature-[a-z]*’ :forbidden-module,] }
  14. Version Catalogs • Define all deps in TOML file •

    Gradle feature to share deps between modules
  15. Version Catalogs [versions] androidx-core = “1.6.0-beta01” [libraries] androidx-core-ktx = {

    module = "androidx.core:core-ktx", version.ref = “androidx-core" }
  16. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } assemble { }
  17. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } assemble { tasks = ["assemble"] }
  18. Gradle Profiler Mean: 348 ms Min: 319 ms P25: 330

    ms Median: 341 ms P75: 368 ms Std dev: 22.71 ms
  19. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } incremental_build { apply-abi-change-to = “src/main/java/StringUtils.kt” }
  20. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } incremental_build { apply-abi-change-to = “src/main/java/StringUtils.kt” apply-android-resource-change-to = “strings.xml” tasks = ["assemble"] }
  21. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } androidStudioSync { android-studio-sync { } }
  22. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } androidStudioSync { android-studio-sync { studio-jvm-args = ["-Xms256m", "-Xmx4096m"] } }
  23. Performance Scenario assemble { # Show a slightly more human-readable

    title in reports title = "Assemble" # Run the 'assemble' task tasks = ["assemble"] } incremental_build { apply-abi-change-to = “src/main/java/StringUtils.kt” tasks = ["assemble"] }
  24. Summary • How to setup & run Gradle Profiler •

    Performance scenarios • Anvil