Skip to content

Speeding up your Android Gradle builds

shewenbiao edited this page Jan 14, 2019 · 13 revisions

About

This was created to demonstrate some of the tips and tricks on how to speed up your Android Gradle builds.

What are the build tips?

  1. Use latest Android Gradle plugin
  2. Avoid legacy multidex
  3. Disable multi-APK
  4. Include minimal resources
  5. Disable png crunching
  6. Use Instant Run
  7. Avoid inadvertent changes
  8. Don’t use dynamic versions
  9. Watch the memory
  10. Enable Gradle caching

1. Use latest Android Gradle plugin

use latest gradle plugin

2. Avoid legacy multidex

avoid legacy multidex

3. Disable multi-APK

disable multi-apk

If you turn it on for your development builds, you'll be wasting time packaging and creating these apks that you're not using. Now, to disable multi-apk generation properly, you can't just disable it in the splits block, because that block is global to all your build variants. So one way you can do it properly is to define a property like here.

diable multi-apk2

Passing devBuild in Android Studio

4. Include minimal resources

include minimal resources

5. Disable png crunching

By default AAPT will crunch your pngs to reduce their size, yielding you a smaller apk. Again, that's a great thing for your release apks. But not that important for your development builds. So avoid png crunching, you can use the same property that we defined, and have, the aaptOptions and cruncherEnabled flag, to disable it if gradle sees that property defined.

disable png crunching

The other option to avoid png crunching is convert all your pngs into webp.

disable png crunching2

6. Use Instant Run

Instant run was launched in Android Studio 2.0. The version of instant run that we are launching with 3.0 is massively than 2.0. Instant run now only work on api 21 or above. That does not mean you can't have your app's min sdk version set to lower. It just means that device you're running Instant app need to be 21 or above. Instead of having sing Instant Run button, the button is now separated into a run and an apply changes button. When you hit the Run button, we will try to do a cold swap, and the app, will always restart. When you click the Apply Changes, we will try to do a hot or warm swap first. And that would push changes directly into the live process. When you use Instant run, it will automatically look at the target device, look at the api level, and look at the set of resources it needs, and automatically built the minimal thing that the target devices requires. So even if you didn't define a separate product flavor, you would automatically get some of the optimizations.

instant run.png

7. Avoid inadvertent changes

avoid inadvertent changes

The code here is basically just using the current date time as version code for your app. Which is a reasonable thing to do, because every time you build, you want to have a unique ID so that your QA team logs a bug, they can tell you which build it was. Now the reason why this is bad is because this would force your Android manifest to change at every build, even the ones that you are not distributing to anybody.

impact of manifest change

Fixing version code updates.

fixing version code updates

8. Don’t use dynamic versions

don't use dynamic versions

Gradle provides a very convenient way to use the latest version of this library,through this plus symbol here. Now it's bad for a couple of reasons.

  1. From a performance perspective, that will make gradle check for new versions of that library every 24 hours, causing your dependency resolution time to increase.
  2. It makes your build nondeterministic. You do a build today compared to a build two weeks from now, you might be building completely different things because the libraries has changed underneath of you.

9. Watch the memory

watch the memory

This dexOptions allow you to set the javaMaxHeapSize for the dex process. This made a lot of sense when dex out of process. But since the plugin 2.1, dex is now in-process by default. So you should not really set this flag anymore. So if you have it, you can just delete it.

10. Enable Gradle caching

enable gradle cache

This is a new caching mechanism from Gradle where you can cache all the task outputs from every task. Now, this is different from the build cache that we introduced in Android Studio 2.3, which only cached pre-dexed external libraries. This cache doesn't only work for the last build, but it works for any previous build from any location. Which means it would really speed up your build when you switch branches, and it will enable distributed caching.

Example: Santa Tracker project

Speeding up Santa Tracker project builds

  1. Use the latest gradle plugin

  1. Avoid legacy multidex

  1. Disable multi-apk

  1. Include minimal resources

  1. Diable png crunching

  1. Use instant run

  1. Use gradle caching

Santa Tracker cumulative improvement

Reference

2017 Google I/O Speeding up your Android Gradle builds Topic

https://juejin.im/post/5be105fde51d455bad089fed (写的还不错的一篇中文文章)

Clone this wiki locally