Friday, April 6, 2018

Configuring Gradle to speed up cordova-cli builds

Leave a Comment

I use Cordova CLI to build my hybrid Android app. In my effort to speed up compile times I have moved the entire Android installation to an SSD and upgraded my machine with an extra 8Gb of memory (total of 16Gb). Nevertheless I see zero effect on compile times which are stuck at pretty much the same figure as they were prior to these upgrades. I am starting to suspect that it is not enough to merely provide more resources. Somehow Gradle needs to be told to use them. However, I am not sure how I do this within the context of a hybrid Cordova app.

Acting on the ideas I found in this Reddit thread I created a gradle.properties file in the app/platforms/android folder where I put

org.gradle.parallel=true org.gradle.daemon=true org.gradle.configureondemand=true org.gradle.jvmargs=-Xms2048m -Xmx8096m -XX:PermSize=1024                     -XX:MaxPermSize=1024m -XX:ReservedCodeCacheSize=1024m 

This did have an effect - it made the build almost 50% slower! The first build with a new Gradle daemon is usually slower so I recompiled. There was a small saving but the build was still slower than before. Clearly, there is more to configuring Gradle than telling it to use tons of memory. However, I have no experience in the domain. Could someone here tell me how I should go about the process in order to get some real performance benefits?

2 Answers

Answers 1

I was recently working on tuning Jenkins pipelines and migrating an app from Maven to Gradle so I hope my knowledge might be useful.

First of all, you can measure difference in execution time in the following way:

  • remove all .gradle folders. It is Gradle's cache folders and it might be accidentally used.
  • execute gradle --stop. It will stop Gradle daemons so that no cache in daemons itself can be used.
  • run gradle clean build

Secondly, the gain that you've got is mostly due to using org.gradle.parallel=true. You may try to remove other things and I bet there will be no significant change in performance. org.gradle.daemon=true is the default value for the option. Also, I would revise JVM flags as e.g. -XX:MaxPermSize is not used in Java 8 (although I understand you're working on Android app)

Thirdly, regarding speeding up the compilation itself, you can use this flags:

GRADLE_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1" 

It should turn off JVM profiling so it might do the trick for you.

Thirdly, check compile vs implementation. Using of implementation configuration theoretically may speed up compilation and recompilation process because of not using transitive dependencies.

I guess I don't know other ways to speed up compilation of a Gradle project. However, there are things like parallel test execution if you're interested in speeding up the whole build.

The last note: check you plugins. Sometimes they do useless things you don't really need (mine bottleneck was wsdl2java) e.g. copying many unused generated sources.

Hope it helps!

Answers 2

Move your project's build folder to SSD: there are tons of write operations during build and write is always much slower than read.

And configure antivirus software (if any) to exclude all java binaries, project's build folder and ~/.gradle from scanning.

These two actions should be enough to see the difference.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment