Faster Android builds

The bigger your app and team get, the slower your build will inevitably become. Here are a few tips to get you building faster. This guide is geared towards anyone responsible for buying machines for Android developers and focuses on rapid iteration.

Raw power

This is your profession, invest in decent tools. While its true that some famous musicians still play cheap guitars, this really doesn't translate to technology. Aim for these specs.

  1. In a desktop, shoot for as many processing cores as you can afford. 6-8 hyper threaded cores beats 4 cores at a higher clock speed. This means an i7 or Xeon desktop like the Dell Precision 7000 or the MacPro.
  2. In a laptop, portability is nice, but often leads to low power processors. i7 is required and avoid anything ending in U, like the i7-5557U found in 13" Macbook Pro. The Dell XPS i7 15" or the 15" Macbook Pro will keep you building fast on the go.
  3. 16Gb RAM minimum. Everything Android related is a memory hog and gets faster the more memory you can give it.
  4. SSD is required. The faster you get files from one place to another, the faster you build and deploy.

Project

Setting up your project correctly can really make a difference.

  1. Break your app into modules. This means properly decoupling chunks of your app into several standalone pieces. These can be built in parallel and even cached. Should your work be isolated to a single module, this will certainly speed things up.
  2. Carefully consider Gradle plugins and annotation processors. These could be slowing down builds because they are figuring stuff out when you build. Obvious but often overlooked.
  3. Configure your build.gradle to exclude things you don't need during rapid development. This cut off between 10%-30% build time in my experience.
  4. Avoid MultiDex if possible.

Gradle configuration

Gradle can be a blessing or a curse and you need to configure it to work optimally. After applying these settings my medium sized project build times went from 37s to 20s (clean assembleDebug)!

To get started, locate your gradle.properties file:

  • Linux: /home/<username>/.gradle/
  • Mac: /Users/<username>/.gradle/
  • Windows: C:\Users\<username>\.gradle

Next add the following lines to gradle.properties:

org.gradle.daemon=true
org.gradle.jvmargs=-Xms256m -Xmx3072m
org.gradle.parallel=true
kotlin.incremental=true

Lastly edit your build.gradle. Below is a starting point and according to the docs, there is a formula to adhere to. From experience you need to try out different settings to ensure you aren't negatively impacting your build times.

android {
  dexOptions {
    maxProcessCount 4
    javaMaxHeapSize "2g"
  }
}