35

I'm looking for a parameter or configuration in Android Studio / Gradle, that can set the build so it will use all of my CPU cores during build time. i.e. If I have a quad-core CPU and each core is running 8 threads, how can I optimize the build so it will use every resource it could get (parallel of course)?

Thank you!

ori888
  • 740
  • 1
  • 8
  • 17

3 Answers3

37

in the local.properties file in my project I have

org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true

additionally you can specify

org.gradle.parallel.threads

which specifies the maximum number of threads to use for parallel execution. it needs org.gradle.parallel=true. Here you can find more information about it

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 1
    thank you for your answer! can you also explain how do I determine the ideal number of threads to use? @blackbelt – ori888 Jun 08 '15 at 10:31
  • 4
    Stumbled across this looking for the opposite, ie. how to prevent android studio from using all the cores and bogging down the machine. I've found that gradle using a couple less than the total is good. (because android studio will also use a core or two as well, in the worst case). – CasualT Jun 25 '15 at 20:49
  • `org.gradle.parallel.threads` is not working any more, use `org.gradle.workers.max` instead. This should be the accepted answer -> https://stackoverflow.com/a/58682140/3568414 – julianwki May 10 '22 at 18:05
  • @julianwki the answer is from 7 years ago. It is only logical that things change – Blackbelt May 11 '22 at 06:28
  • @Blackbelt Things change, correct. SAT, but your answer is outdated, that should be reflected somehow. As long as you don't update it, the other answer is the better one (in the sense of NOT outdated). – julianwki Jun 10 '22 at 16:19
15

TL; DR

According to Gradle Build Environment Documentation:

org.gradle.configureondemand=true
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.workers.max=4             

Details

By default org.gradle.workers.max is set to the number of CPU processors (in my case 8).

Since my Android Studio was freezing my Ubuntu workstation (sometimes forcing me to hard reset the computer), I have set it to 4, ensuring my workstation will not starve on CPU and memory.

JP Ventura
  • 5,564
  • 6
  • 52
  • 69
9

Update for accepted answer:

Since gradle uses Worker API you need to use

org.gradle.workers.max

instead of

org.gradle.parallel.threads

Link to the doc

pitfall
  • 166
  • 1
  • 4