Android Studio gradle takes too long to build
Solution 1
Found the reason!! If Android Studio has a proxy server setting and can't reach the server then it takes a long time to build, probably its trying to reach the proxy server and waiting for a timeout. When I removed the proxy server setting its working fine.
Removing proxy: File > Settings > Appearance & Behavior > System settings > HTTP Proxy
Solution 2
Update 2022; Google removed "Offline work
" option;
They added "Donwload external annotations for dependencies
" option, but of course unchecking that option does not provide the same speed improvements.
Answer
In Android Studio go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
(if on mac) Android Studio -> preferences... -> Build, Execution, Deployment -> Build Tools -> Gradle
Check the 'Offline work' under 'Global Gradle settings'
Note: In newer version of Android studio, View->Tool Windows->Gradle->Toggle button of online/offline
It will reduce 90% gradle build time.
if you just added a new dependency in your gradle you will have to uncheck the offline work or gradle will not be able to resolve the dependencies. After the complete resolving then you you can check the offline work for a faster build
Solution 3
If you are using Google Play services, make sure you aren't using this in your Gradle build script:
compile 'com.google.android.gms:play-services:8.3.0'
Only use those Google APIs that your app is really using. If all you are using is Google Maps, you would use this:
com.google.android.gms:play-services-maps:8.3.0
When I did this, my compile time went from over 2 minutes to around 25 seconds. For a list of the Google apis that you can selectively compile against, see:
https://developers.google.com/android/guides/setup
Solution 4
Found an easy and best way to build gradle
go to your app >> open your gradle.properties and change few lines from it [remove #]
else you may copy paste my below snippet as well
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx2048m -XX:MaxPermSize=512m
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
it worked for me earlier it take almost 2-3 minutes now it is taking only 5-6 second
in above scenario my mac machine have 8GB ram so i assigned 4gb for gradle
Solution 5
Following the steps will make it 10 times faster and reduce build time 90%
First create a file named gradle.properties in the following directory:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
Add this line to the file:
org.gradle.daemon=true
org.gradle.parallel=true
Related videos on Youtube
AndroidDev
Software Maker Software Developer Software Designer Software Tester Software User
Updated on April 25, 2022Comments
-
AndroidDev about 2 years
My Android Studio project used to build faster but now it takes a long time to build. Any ideas what could be causing the delays? I have tried https://stackoverflow.com/a/27171878/391401 but no effect. I haven't any Anti virus running which could interrupt the builds. My app is not that big in size as well (around 5MB) and it used to build within few seconds but not sure what has changed.
10:03:51 Gradle build finished in 4 min 0 sec 10:04:03 Session 'app': running 10:10:11 Gradle build finished in 3 min 29 sec 10:10:12 Session 'app': running 10:20:24 Gradle build finished in 3 min 42 sec 10:28:18 Gradle build finished in 3 min 40 sec 10:28:19 Session 'app': running 10:31:14 Gradle build finished in 2 min 56 sec 10:31:14 Session 'app': running 10:38:37 Gradle build finished in 3 min 30 sec 10:42:17 Gradle build finished in 3 min 40 sec 10:45:18 Gradle build finished in 3 min 1 sec 10:48:49 Gradle build finished in 3 min 30 sec 10:53:05 Gradle build finished in 3 min 22 sec 10:57:10 Gradle build finished in 3 min 19 sec 10:57:11 Session 'app': running
-
Ram Mansawala about 9 yearsWhat do you use, your android phone or any emulator to run your application?
-
AndroidDev about 9 yearsRunning on the device
-
JAvAd about 8 yearsPlease Read this post: stackoverflow.com/questions/22550761/…
-
Neo almost 8 yearsreally to slow... It takes 6 mins to build after cleaning app and at first time.
-
Andrew S over 7 yearsWhen Android studio was new, many on StackOverflow dismissed complaints about this because it was 'only a beta version'. However, its now been some years since it was 'only a beta' and it's still sluggish and that's on 8 cores, 16gb of ram and an SSD disk. The old Eclipse set up on this machine is as fast as a blink.
-
Utsav Gupta over 7 yearsRead this : pissedoff-techie.blogspot.in/2016/01/…
-
zgc7009 over 7 yearsSome of this is also dependent on dependencies that may need to be built.
-
Ahmad Aghazadeh over 7 yearsSee this stackoverflow.com/a/36826562/1770868
-
Aditya Vyas-Lakhan over 7 years
-
-
Thiago over 8 yearsHow to remove the proxy server ?
-
AndroidDev over 8 yearsOn Windows File > Settings > Appearance & Behavior > System settings > HTTP Proxy
-
user3098756 over 8 yearsMy problem was also in proxy, but in that , that I had to add also HTTPS proxy in gradle properties. HTTP proxy was not enough: systemProp.http.proxyHost=xxx.xxx.xxx.xxx systemProp.http.proxyPort=8080 systemProp.https.proxyHost=xxx.xxx.xxx.xxx systemProp.https.proxyPort=8080
-
Sauron about 8 yearsYou deserve 100+ upvotes. Every other source suggests adding
org.gradle.parallel=true
and command line related...but none provide a simple and straightforward understanding of what is really going on. Using your advice, my build time went from 1 min 5 secs, to 3.427 seconds. Awesome!! -
Ramesh about 8 yearsWorked great. From 12 mins to 5 seconds
-
Dany Y about 8 yearswon't this configuration stop fetching new libraries when we add them to build.gradle ?
-
Biswajit Karmakar about 8 yearswhen you add a new library then you have to make it online just for one time which will be notified by gradle build to make project runnable @DanyY
-
Biswajit Karmakar about 8 yearsYou can set it directly from gradle.properties located under studio project.
-
user779370 about 8 yearsYes also enable this option org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
-
Biswajit Karmakar about 8 yearsSet MaxPermSize=1024m
-
The_Martian about 8 yearsAS is complete joke and bs. I am just tired of this nonsense.
-
alnaji about 8 yearsThanks a lot,this was my main problem too. Now everything working great.
-
PC. almost 8 yearsWorks great. Use this link to find your play services library name: developers.google.com/android/guides/…
-
Karue Benson Karue almost 8 yearsYou deserved a tick... You have solved my problem. The problem is on google play services... Thanks :)
-
Josh almost 8 yearsI agree!!! I am suffering unholy amounts of pain to migrate a big project from Eclipse, and now that errors are cleared, building with AS never finishes!!! I hate AS @The_Martian
-
Admin over 7 yearsDoes it mean that all the time the issue was internet speed instead of processor speed?
-
magritte over 7 yearsNot only did this help with the long build issues but I can now also deploy directly to the Android device without using Proguard when debugging (previously I was getting an error about 65K method limit). thanks!
-
magritte over 7 years@Josh I have the same issue... it's been building for 56 minutes now and hasn't finished... did you find a solution? I might switch back to using Eclipse grrr it's taken me ages to migrate the project to AS... I stupidly assumed it would be better / the new shiny.
-
Josh over 7 yearsI just did the typical reset/reopen/build/clean/tweak gradle like mad, and switched to offline and after like 2 hours the build time dropped to normal times. I really don't know what happened. @magritte
-
oli over 7 yearsWhy are those properties not predefined?? Are there any downsides?
-
javad over 7 yearsThis option should only be used with decoupled projects. More details, visit gradle.org/docs/current/userguide/…
-
Neon Warge over 7 yearserrr would it really hurt to just put an error message stating that?
-
Karol Żygłowicz about 7 yearsYes I agree. To enable java 8 in android studio you need to use Jack and by doing this my build times fly to the moon from 1-2 minute builds to 16 minutes... what the .... It is unusable right now. As always great work Google
-
Amod Gokhale about 6 years@AndroidDev - this helps in 2018!!!. Do you know if this is a bug or this is how gradle build should work?
-
Jonathan. almost 6 yearsWhy does it need to connect to a server to build something?
-
Mohd.Zafranudin over 5 yearsBe sure to allow Android Studio on Firewall. Was waiting for almost 15 minutes for the gradle on a fresh install when suddenly Firewall window popup asking me to allow it. Could've been sooner though
-
meyasir over 5 yearsAmazed!! minimize time from 1 min to 2 secs on SSD.
-
Programming Pirate over 5 yearsThis reduced my build time from 30+ minutes to 24 mins... Is there any way to reduce further...?
-
Cardinal System over 4 yearsWoah! Went from over 15 minutes to 1 minute :O
-
drlolly over 4 yearsunfortunately this did not have any effect on my system.
-
David Innocent over 4 yearsAs guys are getting answers...am 11 minutes in waiting for game to finish
-
Fugogugo almost 4 yearsis it only me or there's no Offline Work option anymore in Android Studio 3.6.2?
-
gumuruh almost 4 yearsgreaaaat....!! after 1st compilation now when i switch to offline, everything was blasttt....! great :D
-
Peter over 3 years@Fugogugo - take a look at these posts: stackoverflow.com/questions/57797772/….. and stackoverflow.com/questions/60475046/…
-
Prajwal Waingankar over 3 yearsAs of Android 4.1: Removing proxy:
File > Settings > Appearance & Behavior > System settings > HTTP Proxy
>No proxy
-
Chagai Friedlander almost 3 years@ProgrammingPirate did you find a way? I'm having the same problem and this did not help - I have 32 GB RAM and my computer(Windows) is great just Android Studio is slow
-
Ayxan Haqverdili almost 3 years
-
Victor Sokoliuk over 2 yearsThanks a lot man, I really couldn't to figure out with it by myself.
-
Reza over 2 yearstnx good stack overflow fellow. your simple solution saved a lot of time for me.
-
Asad Mahmood over 2 yearsyes it often happens but once gradle sync process is completed toggle to offline mode if internet speed is very slow unless you have add a new dependency
-
MML over 2 yearsCan you please clear which gradle file I am using
classpath 'com.google.gms:google-services:4.3.10'
this in module app -
Maulik Dodia about 2 years@PrajwalWaingankar I have
No proxy
selected but still I'm facing this issue. Is there any other solution? -
Prajwal Waingankar about 2 yearsUse SSD, atleast 8gb ram, if not an high-end pc: run multitasking.