Tutorial

How to increase your Gradle Build Speed?

Published on August 3, 2022
author

Anupam Chugh

How to increase your Gradle Build Speed?

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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 it’s 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.

  1. 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.

  2. 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.

  3. 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.

  4. Open up the gradle.properties file from the root of your project. Add the following lines of code in it. org.gradle.daemon=true Gradle daemon is a background process. Adding this would consume some extra memory while building. org.gradle.parallel=true The above line of code enables compilation of multiple modules at the same time. Besides that it also gives us other benefits such as;

    • Re-using the configuration for unchanged projects
    • Project-level is up-to-date checks
    • Using pre-built artifacts in the place of building dependent projects

    Adding the following line of code also aids us in speeding up the build. org.gradle.configureondemand=true Another important property is; org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 The above line is used to allow Java compilers to have available memory up to 2 GB (2048 MB). It should only be used if you have available memory more than 2 GB. This is how the gradle.properties file should look like: android gradle properties, increase gradle build speed

  5. 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.

  6. Use only those dependencies that you need. For example google maps dependency, 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'.

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


About the authors
Default avatar
Anupam Chugh

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 20, 2021

Your website is awsome and very helpful. 1. can you add all debugging method process which is mostly used or common. 2. logcat tutorials

- ABC

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 10, 2018

    also add this: org.gradle.caching=true org.gradle.workers.max=(max # of worker processes) When configured, Gradle will use a maximum of the given number of workers. Default is number of CPU processors.

    - alsias

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      April 6, 2018

      Thank you very much brother. God bless you. You have been a great help to many of us having problems to finish our projects. Once again thanks and God bless you and your family.

      - Chuks

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        June 20, 2017

        Thank you . Good article.

        - baser

          Try DigitalOcean for free

          Click below to sign up and get $200 of credit to try our products over 60 days!

          Sign up

          Join the Tech Talk
          Success! Thank you! Please check your email for further details.

          Please complete your information!

          Featured on Community

          Get our biweekly newsletter

          Sign up for Infrastructure as a Newsletter.

          Hollie's Hub for Good

          Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

          Become a contributor

          Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

          Welcome to the developer cloud

          DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

          Learn more
          DigitalOcean Cloud Control Panel