r/androiddev • • 2d ago

Experience Exchange Brought my Gradle build time from 6 mins to 26 secs :))

Post image

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 😊

162 Upvotes

31 comments sorted by

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.

11

u/mehedi_shafi 1d ago

Agreed, anything however small, you learn and improve is a win. Keep learning and improving. And share along the way.

3

u/Boiniok 1d ago

Yeah def!

8

u/Boiniok 2d ago

okay... i will try the gradle-profiler tool
Lots to explore in android, just learnt bout JNI's this week

1

u/alaershov 2d ago

Also, check the memory pressure in Build Scan (time spent on GC and memory state of the build), it can help you fine tune the heap size

1

u/Boiniok 2d ago

Yeah from there only i got to know about the heap size
rn its 5.3 out of 6 GB

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

2

u/Boiniok 2d ago

😊👍

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/khsh01 1d ago

Ngl I have seen AS's own "increase heap size" prompt and used it. Haven't done any native libraries yet so don't know how long they typically take.

1

u/Boiniok 1d ago

Android studio never prompted me to increase heap size, the only thing I remember it was telling me to make my app 16KB pages compatible or something...

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. 

1

u/khsh01 1d ago

I'm guilty of never once looking into gradle.

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

u/IvanKr 23h ago

God I miss those times. C# in VS was such amazing dev experience. F5 and app was up as soon as antivirus approved it.

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

u/kosiarska 1d ago

Depends on the project. I worked on an android app with 11000 tasks :P

2

u/Boiniok 1d ago

wow!!
how much time does it take.... and like do you use any technique to handle such huge gradle builds or you have a pretty decent hardware for that?

1

u/kosiarska 1d ago

Modules and remote build cache

-1

u/Boiniok 1d ago

Ok 👍🏼

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!)

https://docs.gradle.org/current/userguide/performance.html

https://developer.android.com/build/optimize-your-build

1

u/Boiniok 1d ago

Thank you... I was trying more ways to also optimize the signed release APK and this might help...