How to Increase Your Gradle Build Speed
In this tutorial we’ll look at things that can be done with the Gradle build to speed up the build time.
Speed up your Android Gradle Build
As our Android Studio project size increases, the gradle build performance becomes critical. The gradle speed for even the simplest project is pretty slow. Though every project comes up with its own complexity and uniqueness which causes it to have a different build speed. Nevertheless one thing that is pretty common in regard to the build speeds is that it takes our precious time that in return hampers our productivity. A few basic tricks can help us save those extra seconds per build and that makes a big difference to the productivity.
Keep Gradle Updated
Make sure you’re using the latest version of Gradle. Generally with every new update there is a significant improvement in performance. Note: Java 1.8 is faster than 1.6. Make sure it’s updated too.
Minimize the Use of Modules
Try to minimize the use of modules. There are many cases where we need to fork the library to modify it to fit according to our needs. A module takes 4x greater time than a jar or aar dependency. This happens due to the fact that the module needs to be built from the scratch every time.
Enable Gradle Offline Work
Enable gradle Offline Work from Preferences-> Build, Execution, Deployment-> Build Tools-> Gradle. This will not allow the gradle to access the network during build and force it to resolve the dependencies from the cache itself. Note: This only works if all the dependencies are downloaded and stored in the cache once. If you need to modify or add a new dependency you’ll have to disable this option else the build would fail.
Update gradle.properties
Open up the gradle.properties
file from the root of your project. Add the following lines of code in it:
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
These lines enable background processes, parallel compilation of multiple modules, and allow Java compilers to use up to 2 GB of memory. This is how the gradle.properties
file should look like.
Avoid Dynamic Dependencies
Avoid dynamic dependencies such as compile 'com.google.maps.android:android-maps-utils:0.4+'
. Dynamic Dependencies slow down your build since they keep searching for the latest builds every time. To improve the performance we need to fix the version in place.
Use Specific Dependencies
Use only those dependencies that you need. For example, instead of importing compile 'com.google.android.gms:play-services:8.4.0'
, just import compile 'com.google.android.gms:play-services-maps:8.4.0'
.
Conclusion of How to Increase Your Gradle Build Speed
Bringing such tweaks into use in our project saves a lot of time in the long run. I hope these gradle build tips will help you in improving your project build time.