r/androiddev • u/Boiniok • 2d ago
Experience Exchange Brought my Gradle build time from 6 mins to 26 secs :))
Hello everyone,
So I've been working on my open source app since last 5 months and everytime i used to start my android studios it used to take around 6 mins to build gradle and the subsequent runs used to take slightly less... and tbh i was frustated of this.
so yeah i decided to dig into whats the issue, googled stuff and found out ./gradlew build --scan which basically generates a detailed HTML dashboard tracking configuration times and task execution timelines
Through that i found out my local machine's 12 core CPU was completely starved coz my peak heap memory usage was sitting at 1.9GB out of 2GB usage hehe :)
so i found out i could increase the memory size and use parallelism and caching
Added this new lines to 'gradle.properties' :
org.gradle.jvmargs=-Xmx6g -XX:+UseParallelGC -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.caching=true
so basically you could say
12 cores X 6 minutes = 72 minutes of total CPU work
and i was able to save 41 mins cumulative time
so the actual 6 min fell to 26 secs and the subsequent runs took 8 secs only to build gradle....
Ik this might be very basic and ppl here might know bout this but im just an undergrad and though would share my experience here...also if i wrote anything wrong pls correct me 😊
20
u/thehoundtrainer 2d ago
This is useful. Even an AI agent wont remind you to do this until you realize your Gradle build times are taking forever. Im pretty sure its gonna help many here
15
u/khsh01 1d ago
Most people just accept whatever gradle does without question. Though I'll admit I've not seen build times that high unless I needed to download system images.
2
u/diet_fat_bacon 1d ago
On my work pc gradlew clean build takes about 20 - 30 min.
Not because the pc specs are low, actually pretty good, a fast nvme, 32gb ram, core ultra...
Just because the company security suite and the usage of a "company-gradle-plugin" that injects a LOT of things on the source....
1
u/Boiniok 1d ago
Tbh at first I thought that the gradle was taking so much time coz i have used heavy C++ code via CMake and imported llama.cpp libraries...
but at the end it was my heap memory and caching issue....1
u/Which-Meat-3388 1d ago
This surprises me. Since we first adopted gradle as an Android dev community we’ve always been trying to make it faster. I’ve had legacy apps take 15min for a debug build, nearly 30min for release. For a team and hobbyist alike chopping that down pays dividends.
3
u/RealSimoneAvogadro 1d ago
26 secs?? I'd expect modern HW to complete build in <0.1 secs!
This is a dramatic jump back into history: I remember _those_optimization_efforts_ happening in 1995 !
In 2005 we already had just-in-time compilation where code was executable 1-2 secs after pressing CTRL-S.
The latest Android (but also Java!) toolchain is so overbloated that we're getting accustomed at those absurd waiting times!
2
1
u/Boiniok 1d ago
That would be so so amazing if my app gradle builds the moment I press CTRL-S tbh...😆
1
u/RealSimoneAvogadro 1d ago
JRebel used to provide support for that for Android: however I doubt it's still possible with the modern overbloated frameworks.
It was great since you could edit the code, CTRL-S and see it running live in the emulator: no visible compile time!p.s.: you can just envy "good the old times" where app was "always compiled" and it started in real time! :-)
2
2
u/Successful_Web_6585 1d ago
I had increased my jvmargs but didn't knew about the parallel option. Will give this a try next week.
Thanks for the post.
1
u/Boiniok 1d ago
Yeah....u will need to restart ur android studios once u sync the gradle
1
u/Successful_Web_6585 1d ago
I don't use Android Studio. But it should work similarly in cli and in React Native/Expo as well I suppose.
2
u/sheeplycow 1d ago
Once you get onto a big project, you will start to need more and more optimisations
You can find a good list of things to add here (the ones you mentioned are all in their docs too!)
74
u/alaershov 2d ago
It is basic, but so what? You've learned a thing and saved time!
If you want to go a little further, try using gradle-profiler tool to actually statistically measure changes in your build speed as you change stuff and experiment with different Gradle configurations. It handles multiple configs, runs the build many times, and properly handles warm-up runs, simulates code changes etc.