Android Studio gradle takes too long to build

333,248

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.

check this to reduce 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
Share:
333,248

Related videos on Youtube

AndroidDev
Author by

AndroidDev

Software Maker Software Developer Software Designer Software Tester Software User

Updated on April 25, 2022

Comments

  • AndroidDev
    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
      Ram Mansawala about 9 years
      What do you use, your android phone or any emulator to run your application?
    • AndroidDev
      AndroidDev about 9 years
      Running on the device
    • JAvAd
      JAvAd about 8 years
    • Neo
      Neo almost 8 years
      really to slow... It takes 6 mins to build after cleaning app and at first time.
    • Andrew S
      Andrew S over 7 years
      When 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
      Utsav Gupta over 7 years
    • zgc7009
      zgc7009 over 7 years
      Some of this is also dependent on dependencies that may need to be built.
    • Ahmad Aghazadeh
      Ahmad Aghazadeh over 7 years
    • Aditya Vyas-Lakhan
      Aditya Vyas-Lakhan over 7 years
  • Thiago
    Thiago over 8 years
    How to remove the proxy server ?
  • AndroidDev
    AndroidDev over 8 years
    On Windows File > Settings > Appearance & Behavior > System settings > HTTP Proxy
  • user3098756
    user3098756 over 8 years
    My 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
    Sauron about 8 years
    You 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
    Ramesh about 8 years
    Worked great. From 12 mins to 5 seconds
  • Dany Y
    Dany Y about 8 years
    won't this configuration stop fetching new libraries when we add them to build.gradle ?
  • Biswajit Karmakar
    Biswajit Karmakar about 8 years
    when 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
    Biswajit Karmakar about 8 years
    You can set it directly from gradle.properties located under studio project.
  • user779370
    user779370 about 8 years
    Yes also enable this option org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
  • Biswajit Karmakar
    Biswajit Karmakar about 8 years
    Set MaxPermSize=1024m
  • The_Martian
    The_Martian about 8 years
    AS is complete joke and bs. I am just tired of this nonsense.
  • alnaji
    alnaji about 8 years
    Thanks a lot,this was my main problem too. Now everything working great.
  • PC.
    PC. almost 8 years
    Works great. Use this link to find your play services library name: developers.google.com/android/guides/…
  • Karue Benson Karue
    Karue Benson Karue almost 8 years
    You deserved a tick... You have solved my problem. The problem is on google play services... Thanks :)
  • Josh
    Josh almost 8 years
    I 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
    Admin over 7 years
    Does it mean that all the time the issue was internet speed instead of processor speed?
  • magritte
    magritte over 7 years
    Not 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
    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
    Josh over 7 years
    I 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
    oli over 7 years
    Why are those properties not predefined?? Are there any downsides?
  • javad
    javad over 7 years
    This option should only be used with decoupled projects. More details, visit gradle.org/docs/current/userguide/…
  • Neon Warge
    Neon Warge over 7 years
    errr would it really hurt to just put an error message stating that?
  • Karol Żygłowicz
    Karol Żygłowicz about 7 years
    Yes 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
    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.
    Jonathan. almost 6 years
    Why does it need to connect to a server to build something?
  • Mohd.Zafranudin
    Mohd.Zafranudin over 5 years
    Be 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
    meyasir over 5 years
    Amazed!! minimize time from 1 min to 2 secs on SSD.
  • Programming Pirate
    Programming Pirate over 5 years
    This reduced my build time from 30+ minutes to 24 mins... Is there any way to reduce further...?
  • Cardinal System
    Cardinal System over 4 years
    Woah! Went from over 15 minutes to 1 minute :O
  • drlolly
    drlolly over 4 years
    unfortunately this did not have any effect on my system.
  • David Innocent
    David Innocent over 4 years
    As guys are getting answers...am 11 minutes in waiting for game to finish
  • Fugogugo
    Fugogugo almost 4 years
    is it only me or there's no Offline Work option anymore in Android Studio 3.6.2?
  • gumuruh
    gumuruh almost 4 years
    greaaaat....!! after 1st compilation now when i switch to offline, everything was blasttt....! great :D
  • Peter
    Peter over 3 years
  • Prajwal Waingankar
    Prajwal Waingankar over 3 years
    As of Android 4.1: Removing proxy: File > Settings > Appearance & Behavior > System settings > HTTP Proxy > No proxy
  • Chagai Friedlander
    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
    Ayxan Haqverdili almost 3 years
  • Victor Sokoliuk
    Victor Sokoliuk over 2 years
    Thanks a lot man, I really couldn't to figure out with it by myself.
  • Reza
    Reza over 2 years
    tnx good stack overflow fellow. your simple solution saved a lot of time for me.
  • Asad Mahmood
    Asad Mahmood over 2 years
    yes 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
    MML over 2 years
    Can you please clear which gradle file I am using classpath 'com.google.gms:google-services:4.3.10' this in module app
  • Maulik Dodia
    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
    Prajwal Waingankar about 2 years
    Use SSD, atleast 8gb ram, if not an high-end pc: run multitasking.