Continuous Integration #1: How to speed up gradle builds

It’s absolutely great that continuous integration become a standard lately but the success of this approach is determined by feedback loop time. No one would take builds&testing on remote server seriously if there is a need of waiting an hour or even longer for build results. Here you can find some tips&tricks making your builds much faster if you chose gradle as your build tool.

1.Use profiler to determine where the problem is.
Gradle can provide you quite extensive information what tasks take the most time during the build. Simple add —profile at the end of the build command:

./gradlew clean build —profile

and open the link from the output in the browser.

2. Remove unused dependencies from your build.
Loading libraries and tools from repository takes time so try to keep the dependency list up to date.

3. Try parallel builds.
If you have couple of modules in the project probably some of them could be run in parallel. Simply add gradle.properties file into your root project with 

If you have couple of modules in the project probably some of them could be run in parallel. Simply add gradle.properties file into your root project with 

.org.gradle.parallel=true

and check how much time can you save. But be careful. Sometimes when you have custom tasks that depend on each other gradle can put them in wrong order. It’s quite safe in general but to be sure run build couple of times.

4. Avoid putting too much logic in configuration stage of tasks.
Remember that if you put:

task myTask() {
    //some logic here
    println “test”
}

Your code will be executed always on the configuration stage no matter what task you execute. Try 

./gradlew clean

As you can see content of your task has been executed. To avoid such situations put your logic into doFirst or doLast statements

task myTask(){
    doLast {
        // some logic here
    }
}

5. Separate your CI build task from local build task.
Often you want to generate and analyze some reports during development stage but never you read them on the CI server. In that case there is no reason for building these reports there and burning time for nothing. Simply create new task build-ci and optimize it as much as you can.

6. Keep your build script clean.
When your build script file is quite messy and contains a lot of legacy or dead code you probably may experience some slowdown. So try to refactor your code and keep it clean.

7. Update your build tool to the newest version.
Gradle put some effort to optimize itself so it’s quite smart to follow their updates.

That’s all for the beginning. This simpler 7 steps should improve noticeably feedback loops from your CI servers. If you want to dig deeper in this topic try reading this article: https://guides.gradle.org/performance/
Enjoy 😉

Leave a Reply

Your email address will not be published. Required fields are marked *