-
Notifications
You must be signed in to change notification settings - Fork 0
Speeding up your Android Gradle builds
This was created to demonstrate some of the tips and tricks on how to speed up your Android Gradle builds.
- Use latest Android Gradle plugin
- Avoid legacy multidex
- Disable multi-APK
- Include minimal resources
- Disable png crunching
- Use Instant Run
- Avoid inadvertent changes
- Don’t use dynamic versions
- Watch the memory
- Enable Gradle caching
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.
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.
The other option to avoid png crunching is convert all your pngs into webp.
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.
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.
Fixing version code updates.
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.
- 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.
- 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.
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.
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.
- Use the latest gradle plugin
- Avoid legacy multidex
- Disable multi-apk
- Include minimal resources
- Diable png crunching
- Use instant run
- Use gradle caching
2017 Google I/O Speeding up your Android Gradle builds Topic
https://juejin.im/post/5be105fde51d455bad089fed (写的还不错的一篇中文文章)