Slide 1

Slide 1 text

DEMYSTIFY & OPTIMIZE YOUR ANDROID GRADLE BUILDS

Slide 2

Slide 2 text

Mobiconf 2021 Demystify & Optimize Your Android Gradle Builds Android Developer & Instructor Kotlin GDE @n8ebel goobar.dev Nate Ebel

Slide 3

Slide 3 text

What is Gradle?

Slide 4

Slide 4 text

What is Gradle? A. The source of 94.3% of Android dev Twitter tra ffi c B. A viable means of heating a small room C. A general purpose build tool used for Android and other projects D. All of the above

Slide 5

Slide 5 text

What is Gradle? A. The source of 94.3% of Android dev Twitter tra ffi c B. A viable means of heating a small room C. A general purpose build tool used for Android and other projects D. All of the above

Slide 6

Slide 6 text

Android Builds Android Gradle Plugin Gradle

Slide 7

Slide 7 text

Android Builds Android Gradle Plugin Gradle

Slide 8

Slide 8 text

Android Builds Gradle & the Android Gradle Plugin are the backbone of most Android projects today

Slide 9

Slide 9 text

Android Builds Backbone of our Android projects

Slide 10

Slide 10 text

Android Builds Backbone of our Android projects

Slide 11

Slide 11 text

Android Builds Backbone of our Android projects Source of frustration and lost productivity

Slide 12

Slide 12 text

Builders want to Build

Slide 13

Slide 13 text

Developer Productivity Engineering

Slide 14

Slide 14 text

Developer Productivity Engineering “The use of data analytics and build acceleration technologies to speed up critical software development processes - and to make them more e ff i cient.” https://gradle.com/blog/top-three-reasons-to-launch-a-dedicated-developer- productivity-engineering-team/

Slide 15

Slide 15 text

Where are we going? Understanding Profiling Optimizing Maintaining

Slide 16

Slide 16 text

My hope for you 1. Better understand what happens when you build your project 2. Identify at least 1 potential tool or optimization to try out today

Slide 17

Slide 17 text

Understanding Your Build

Slide 18

Slide 18 text

Run ‘app’ ?

Slide 19

Slide 19 text

Executing tasks [:app:assembleDebug]

Slide 20

Slide 20 text

Gradle Task “… a single atomic piece of work for a build, such as compiling classes or generating javadoc.” https://docs.gradle.org/current/dsl/org.gradle.api.Task.html

Slide 21

Slide 21 text

Where do Tasks come from?

Slide 22

Slide 22 text

Where to Tasks come from? • From Gradle • From our own custom Tasks • From Plugins

Slide 23

Slide 23 text

Tasks coming from Gradle • help • dependencies • tasks • projects • properties

Slide 24

Slide 24 text

Custom Gradle Tasks De fi ne your own tasks to perform whatever task you need Custom Tasks task clean(type: Delete) { delete rootProject.buildDir } task helloWorld { println("Hello World") }

Slide 25

Slide 25 text

Tasks coming from Plugins • assembleDebug • artifactoryPublish • appDistributionUploadDebug

Slide 26

Slide 26 text

Gradle Plugins “[… plugins add new tasks, domain objects, conventions, and extend core objects…]” https://docs.gradle.org/current/userguide/plugins.html

Slide 27

Slide 27 text

Custom Gradle Plugins De fi ne your own Gradle plugins for common con fi guration or custom tasks Custom Tasks class VersionCodePlugin : Plugin { override fun apply(project: Project) { project.version = calculateVersion() … } } apply plugin: VersionCodePlugin

Slide 28

Slide 28 text

What does the most basic Gradle project look like?

Slide 29

Slide 29 text

The interesting functionality comes from Gradle Plugins

Slide 30

Slide 30 text

What tasks are available in a brand new Android project?

Slide 31

Slide 31 text

Android Gradle Plugin

Slide 32

Slide 32 text

What is the relationship? Android Gradle Plugin Gradle

Slide 33

Slide 33 text

Slow Builds?

Slide 34

Slide 34 text

Slow builds come from • Underpowered hardware • Computationally intensive tasks • Building more than is needed

Slide 35

Slide 35 text

Faster Machines == Faster Builds

Slide 36

Slide 36 text

Avoid computationally intensive tasks when possible

Slide 37

Slide 37 text

Avoid doing more work than is necessary

Slide 38

Slide 38 text

Phases of Gradle Builds Initialization Configuration Execution

Slide 39

Slide 39 text

Initialization

Slide 40

Slide 40 text

Configuration

Slide 41

Slide 41 text

Initialization & Configuration

Slide 42

Slide 42 text

Execution

Slide 43

Slide 43 text

Execution Task Input Output

Slide 44

Slide 44 text

Task Execution Status • EXECUTED • UP-TO-DATE • FROM-CACHE • SKIPPED • NO-SOURCE

Slide 45

Slide 45 text

Common Build Scenarios First Time Build Up-To-Date Build Clean Build Incremental Build

Slide 46

Slide 46 text

First Time Build • Fresh checkout of a project • No previously built outputs/artifacts • May pull from a remote cache

Slide 47

Slide 47 text

Up-To-Date Build • Run a task • Run the same task again with no changes • Ideally, nothing should run • Everything should be UP-TO-DATE

Slide 48

Slide 48 text

Clean Build • Delete local build artifacts • Run your task again • May need to rebuild full project • Should ideally pull many artifacts from build cache

Slide 49

Slide 49 text

Incremental Build • Make a change to your project code/resources • Run your task again • Ideally, only a portion of your project should need to rebuild

Slide 50

Slide 50 text

Execute as few tasks as possible for any given build

Slide 51

Slide 51 text

The Story So Far • Click ‘run app’ • Android Studio executes an Android Speci fi c Gradle task • That task comes from Android Gradle Plugin • That task will run via Gradle’s infrastructure • Gradle is designed to avoid doing unnecessary work • Our goal, then, is to con fi gure our builds to do as little work as possible

Slide 52

Slide 52 text

Profiling Your Build

Slide 53

Slide 53 text

What gets measured, gets improved

Slide 54

Slide 54 text

Profiling/Monitoring Tools Gradle Build Output Android Studio Build Analyzer Gradle Buildscans Gradle Enterprise

Slide 55

Slide 55 text

Gradle Build Output • Build time • Number of tasks executed • Number of tasks from cache • Number of tasks up-to-date • Task status

Slide 56

Slide 56 text

Android Studio Build Analyzer • Build overview • Plugin analysis • Task execution time • Warnings & recommendations

Slide 57

Slide 57 text

Profiling Across Machines

Slide 58

Slide 58 text

Gradle Buildscans • Detailed report of a speci fi c build • Easily shareable between team members and across machines • Can extend to fi t the needs of your team

Slide 59

Slide 59 text

Gradle Buildscans • Con fi guration, Initialization, and Execution time • Queryable build timeline • Console outputs • Project structure • Environment details • Cache hit/miss report • Test outputs

Slide 60

Slide 60 text

How to Generate a Buildscan? • add --scan to the end of your Gradle Wrapper invocation • accept the TOS • click the generated link and enter an email • click the link in the email to view your scan • subsequent scans won’t require the email

Slide 61

Slide 61 text

How can we use a Buildscan? • Analyze con fi guration and initialization times to ensure a properly con fi gured build • Analyze task execution to identify slow running tasks • Use the timeline to determine if any cacheable tasks were executed • Diagnose build failures for distributed teammates • Diagnose CI build failures • Identify hardware factors contributing to slow builds

Slide 62

Slide 62 text

Analyzing Trends

Slide 63

Slide 63 text

Gradle Enterprise

Slide 64

Slide 64 text

Gradle Enterprise “Gradle Enterprise leverages acceleration technologies to speed up the software build and test process and data analytics to make troubleshooting more e ff i cient” https://gradle.com/

Slide 65

Slide 65 text

What does Gradle Enterprise do? • Store buildscans on remote server for historical access and analysis • Automatically store buildscans in a single location • Provides a remote build cache • Analyze build trends over time • Enables support for custom tagging and fi ltering • Supports test distribution and analysis

Slide 66

Slide 66 text

Gradle Enterprise at Premise • Identify build cache issues across local and CI builds • Identify regionally speci fi c build cache issues • Identify build speed regressions due to AGP and NDK • Quickly diagnose build failures for distributed team • Compare buildscans to diagnose build issues

Slide 67

Slide 67 text

Gradle Enterprise at Premise • Is a paid product • Is an incredibly valuable tool for our team

Slide 68

Slide 68 text

Optimizing Your Build

Slide 69

Slide 69 text

Give your devs faster machines

Slide 70

Slide 70 text

Slow build times * cost/dev = = money for faster hardware

Slide 71

Slide 71 text

Enable build acceleration features

Slide 72

Slide 72 text

• Parallel execution • Local build cache • Remote build cache • Con fi guration cache • File-system watching Build acceleration features

Slide 73

Slide 73 text

Parallel Execution • Build projects in parallel • Very bene fi cial in multi-modular projects with reasonable dependency graph • Add org.gradle.parallel=true to gradle.properties

Slide 74

Slide 74 text

Gradle Build Cache • Reuse the outputs from previous builds even after a clean • Local Cache • Add org.gradle.caching=true to your gradle.properties fi le • Remote Cache • Setup with, or without, Gradle Enterprise

Slide 75

Slide 75 text

Configuration Caching • Cache con fi guration data • Add org.gradle.unsafe.con fi guration-cache=true to gradle.properties • Very experimental / Plugins must support it https://github.com/gradle/gradle/issues/13490

Slide 76

Slide 76 text

File System Watching • Reduce I/O impact of builds using in-memory fi le system info • Add org.gradle.vfs.watch=true to gradle.properties • On by default in Gradle 7+

Slide 77

Slide 77 text

Optimize memory settings

Slide 78

Slide 78 text

Optimize memory settings • Allocate more memory to Gradle daemon • Add org.gradle.jvmargs=-Xmx2048M to gradle.properties • Allocate more memory to Kotlin compiler • Add -Dkotlin.daemon.jvm.options=-Xmx2048M to gradle.properties • Play with this setting based on your build and hardware

Slide 79

Slide 79 text

Optimize memory settings • Remember that builds run in environments with limited resources • (Gradle daemon + Kotlinc) * numberOfSeparateBuildProcesses = total memory • Your machine, your teammate’s machine, and your CI machine might di ff er

Slide 80

Slide 80 text

Optimize how you build

Slide 81

Slide 81 text

Optimize how you build • Use a single JDK • Keep libraries and tooling up to date • Build only what is needed • Modularize your project • Reduce the use of kapt

Slide 82

Slide 82 text

Use the same JDK • Use the same JDK for Android Studio and command line builds • Set JAVA_HOME environment variable • Con fi gure Android Studio to use that JDK rather than embedded • This will speed up builds when switching from cmd line & Android Studio

Slide 83

Slide 83 text

Keep libraries up-to-date • Stay up to date with libraries, plugins, languages • Helps pick up performance improvements, cache miss fi xes, etc

Slide 84

Slide 84 text

Build only what is needed • Don’t minify your app if you don’t need to • Don’t build multiple variants if you don’t need them all • Exclude locales/resources for dev builds when not needed • Disable plugins such as Crashlytics if not needed for dev builds • Build/test/analyze in parallel when possible

Slide 85

Slide 85 text

Modularize your project • Modules/projects can build in parallel • Splitting your project into smaller modules enables greater parallelization • Limited bene fi t when most of the code is all in a single module • Limited bene fi t if all modules are highly coupled

Slide 86

Slide 86 text

Reduce the use of kapt • Kapt is very slow for Kotlin builds • Avoid annotation processors with kapt if possible • Limit to speci fi c modules if possible • Consider using KSP if applicable for your project

Slide 87

Slide 87 text

Maintaining Your Build

Slide 88

Slide 88 text

Monitor and analyze your build

Slide 89

Slide 89 text

Gradle Enterprise

Slide 90

Slide 90 text

Gradle Doctor

Slide 91

Slide 91 text

Gradle Doctor • Plugin to diagnose & prevent common build issues • Con fi gurable warnings for things such as • Build speed issues • Annotation processing time • Mismatched JDK versions • Building all variants • Remote cache speed issues https://runningcode.github.io/gradle-doctor/

Slide 92

Slide 92 text

android-gradle-cache-fix Plugin

Slide 93

Slide 93 text

android-gradle-cache-fix Plugin • Plugin to fi x known build cache issues in Android Gradle Plugin • Add to your project and start seeing more cache hits https://github.com/gradle/android-cache- fi x-gradle-plugin

Slide 94

Slide 94 text

Benchmark your builds

Slide 95

Slide 95 text

Gradle Profiler

Slide 96

Slide 96 text

Gradle Profiler • Run multiple Gradle builds and aggregate the results • Highlights statistically signi fi cant changes to build performance • Use to pro fi le the impact of build changes such as enabling caching • Can be run manually or automated for common scenarios https://github.com/gradle/gradle-pro fi ler

Slide 97

Slide 97 text

Gradle Profiler

Slide 98

Slide 98 text

Keep your project up-to-date • Stay up to date with libraries, plugins, languages • Helps pick up performance improvements, cache miss fi xes, etc

Slide 99

Slide 99 text

Make your build easier to work with

Slide 100

Slide 100 text

Wrapping Up

Slide 101

Slide 101 text

Android relies on Android Gradle Plugin and Gradle to build our projects

Slide 102

Slide 102 text

Gradle provides infrastructure and plugins provide functionality

Slide 103

Slide 103 text

There’s very little magic

Slide 104

Slide 104 text

To speed up our builds, we can use faster hardware, do less intensive work, or do less work.

Slide 105

Slide 105 text

Tools such as buildscans and Gradle Enterprise help diagnose build issues

Slide 106

Slide 106 text

We can optimize our builds through caching, modularization, and other build acceleration strategies

Slide 107

Slide 107 text

By monitoring and profiling performance, we may maintain efficiency over time

Slide 108

Slide 108 text

Develop a culture of continued investment into developer productivity for happier, more productive, teams.

Slide 109

Slide 109 text

Thank You @n8ebel goobar.dev