It’s been some time since Google I/O 2017 was released and there are lots of new things for Android developers. We all have to suffer from the Gradle build speed issue. Sometimes it takes ages to build an APK. But in this I/O session, Google team presented some cool tips on how you can speed up your Gradle builds and it looks promising. After listening to the complete session, I decided to extract some cool tips for your reference.
1. Always use the latest Gradle plugin for Android
The tool team is working hard to improve the performance of the Gradle plugin for Android, so it’s better to check the latest build and always keep updating.
2. Don’t use legacy MultiDex
If you are using legacy MultiDex in a device having a lower Android version, then it can impact your app performance. Always try to use the latest version of MultiDex so that it can be supported by all Android versions without impacting the performance of the app.
3. Turn Off multi-build APK
If you are in development phase, then it would be better to disable this as it slows down build time. You can disable this using the below-mentioned script.
You can also set this using Android Studio. For this, open Preferences -> Build, Execution, Deployment -> Compiler settings in Android Studio and add -PdevBuild to the Command-line options.
4. Use only required resources
Resources occupy some space while generating APK file and also impact the build time. If you are in development phase, then let Gradle know to only take care of the required resources. Also, you can set the preference of the device for which you are developing the application.
5. Turn off PNG grinding
PNG optimization is not necessary for the development builds so you can turn it off, as by default it is on. It will definitely speed up the build time.
6. Give Instant Run a try
There is a bad reputation of the instant run, but when it works, it works like a charm. The tooling team has worked on it and now in Android 3.0, it is more stable as compared to earlier versions. So you can give it a try and speed up the build performance.
7. Avoid using dynamic dependencies
Using dynamic dependency version causes Gradle to check for the new version in every 24 hours. So it’s highly recommended not to use the dynamic dependency.
8. Always take care of memory
You should keep your eyes on how much memory you are giving to the Gradle. You can watch this video to know more on Gradle memory setting and Dex in the process.
9. Turn on Gradle caching
Gradle caching is introduced in Gradle version 3.5. It reuses the outputs from the previous build. It can work across the projects and gives a high performance when used with Android Studio 3.0. So let’s give it a try as well.
10. Disable Crashlytics plugin
We generally add crashlytics plugin in Android Studio to find the cause of the issue/crash, but it slows down the build time as it generates new ID every time we generate a build. It’s better to disable this in development builds using below-mentioned code snippet.
You can also check this official link with the list of resources that will help you to optimize your build speed. Happy coding. 🙂