How To Speed Up Your Gradle Build

Slow Gradle build? Try disabling anti-virus protection for your Gradle caches directory and your Android Studio project directory. (Gradle caches directory: ~/.gradle/caches.) If you're on Windows, make sure those same directories are excluded from Windows Search indexing, too. For Mac users, excluding those directories from Spotlight is a good idea. Details on which directories to include can be found here.
Developing Android apps on Windows, I've been frustrated by all the time lost to long Gradle builds. Today, I discovered that adding anti-virus exclusions on my cache and build directories cut my clean-rebuild time by roughly 50%. Wow! How is this not the first thing on every Gradle optimization guide? The Windows Search indexing exclusion didn't make quite as big an impact, but it was also pretty major. I've included instructions for Windows 10 below.
Another tip: put Gradle into offline mode while you're coding away and not adding new dependencies. This prevents Gradle from checking for updates when you know there aren't any, or don't care if there are.
In addition, there are lots of Gradle optimizations you can enable. This guide covers the topic in great depth. I use these flags in my ~/.gradle/gradle.properties file.
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx6144m <-- Tweak this based on available RAM
org.gradle.caching=true
org.gradle.parallel=true
kotlin.incremental=true
Note that enabling caching means you sometimes have to explicitly clear your caches when switching branches. I run this script when I run into puzzling build issues.
#!/bin/bash

# Clean Android cache
./gradlew cleanBuildCache

# Clean Gradle cache, prompting for each directory
find ~/.gradle/caches -maxdepth 1 -name build-cache* -print -exec rm -rfI {} \;

# Clean Project
./gradlew clean

# Stop Gradle Daemon
./gradlew --stop

You can read more about the various caches in this excellent article: Exploring the Android build process Caching – The ASOS Tech Blog – Medium.

Windows Defender - How to Exclude a Directory
  1. Open Windows Defender Settings
  2. Click "Virus & threat protection"
  3. Click "Virus & threat protection settings"
  4. Scroll down to "Exclusions" and click "Add or remove exclusions"
  5. Click "Add an exclusion" and select "Folder" from the drop-down menu
  6. Navigate to the directory to be excluded, or paste its path into the "Folder" text-box, and click "Select Folder"

Windows Search Indexing - How to Exclude A Directory
  1. Open Windows Control Panel
  2. Search for "index" and choose "Indexing Options"
  3. Click the "Modify" button
  4. Under the appropriate drive letter, navigate to the folder you want to exclude, and make sure it is unchecked
  5. Double-check your work by verifying the directory shows up in the "Exclude" column of the "Summary of selected locations" table

Tip: In Windows File Explorer, you can SHIFT-right-click on a folder and choose "Copy as Path" to copy its full path to the clipboard. This can then be pasted into any standard Windows open-file or select-folder dialog to speed up navigation.

If you found this helpful, you'd like to thank me, and you enjoy reading -- or know someone who does -- buy one of my books!


comments powered by Disqus