Android Studio gradle takes too long to build
Asked Answered
G

24

280

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://mcmap.net/q/110195/-gradle-take-too-much-to-build 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  
Goulette answered 1/4, 2015 at 12:55 Comment(9)
What do you use, your android phone or any emulator to run your application?Jedediah
Running on the deviceGoulette
Please Read this post: #22551261Glazunov
really to slow... It takes 6 mins to build after cleaning app and at first time.Period
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.Keister
Read this : pissedoff-techie.blogspot.in/2016/01/…Ceyx
Some of this is also dependent on dependencies that may need to be built.Contract
See this https://mcmap.net/q/79950/-building-and-running-app-via-gradle-and-android-studio-is-slower-than-via-eclipseKunming
#41502664Rissole
G
47

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

Goulette answered 2/4, 2015 at 9:24 Comment(13)
How to remove the proxy server ?Leodora
On Windows File > Settings > Appearance & Behavior > System settings > HTTP ProxyGoulette
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=8080Remde
AS is complete joke and bs. I am just tired of this nonsense.Tartaric
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 @TartaricAloeswood
@Aloeswood 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.Royceroyd
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. @RoyceroydAloeswood
errr would it really hurt to just put an error message stating that?Desperation
Why does it need to connect to a server to build something?Lapse
As guys are getting answers...am 11 minutes in waiting for game to finishMohammedmohammedan
As of Android 4.1: Removing proxy: File > Settings > Appearance & Behavior > System settings > HTTP Proxy > No proxyJostle
@PrajwalWaingankar I have No proxy selected but still I'm facing this issue. Is there any other solution?Agnesse
Use SSD, atleast 8gb ram, if not an high-end pc: run multitasking.Jostle
C
502

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

Clypeate answered 8/3, 2016 at 5:18 Comment(8)
won't this configuration stop fetching new libraries when we add them to build.gradle ?Areola
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 @DanyYClypeate
Does it mean that all the time the issue was internet speed instead of processor speed?Juvenilia
unfortunately this did not have any effect on my system.Merlon
is it only me or there's no Offline Work option anymore in Android Studio 3.6.2?Warrenne
greaaaat....!! after 1st compilation now when i switch to offline, everything was blasttt....! great :DDeaf
@Warrenne - take a look at these posts: #57798272.. and #60475546Courtnay
Related: Cannot enable Gradle's offline mode on Android Studio 3.6Glaring
S
162

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

Shreveport answered 29/11, 2015 at 1:37 Comment(7)
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!!Seawright
Thanks a lot,this was my main problem too. Now everything working great.Furze
Works great. Use this link to find your play services library name: developers.google.com/android/guides/…Hyden
You deserved a tick... You have solved my problem. The problem is on google play services... Thanks :)Pinkie
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!Royceroyd
@Goulette - this helps in 2018!!!. Do you know if this is a bug or this is how gradle build should work?Beguine
Can you please clear which gradle file I am using classpath 'com.google.gms:google-services:4.3.10' this in module appTeno
W
58

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

Wishywashy answered 27/5, 2016 at 5:41 Comment(0)
G
47

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

Goulette answered 2/4, 2015 at 9:24 Comment(13)
How to remove the proxy server ?Leodora
On Windows File > Settings > Appearance & Behavior > System settings > HTTP ProxyGoulette
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=8080Remde
AS is complete joke and bs. I am just tired of this nonsense.Tartaric
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 @TartaricAloeswood
@Aloeswood 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.Royceroyd
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. @RoyceroydAloeswood
errr would it really hurt to just put an error message stating that?Desperation
Why does it need to connect to a server to build something?Lapse
As guys are getting answers...am 11 minutes in waiting for game to finishMohammedmohammedan
As of Android 4.1: Removing proxy: File > Settings > Appearance & Behavior > System settings > HTTP Proxy > No proxyJostle
@PrajwalWaingankar I have No proxy selected but still I'm facing this issue. Is there any other solution?Agnesse
Use SSD, atleast 8gb ram, if not an high-end pc: run multitasking.Jostle
E
32

In Android Studio, above Version 3.6, There is a new location to toggle Gradle's offline mode To enable or disable Gradle's offline mode.

To enable or disable Gradle's offline mode, select View > Tool Windows > Gradle from the menu. In the top bar of the Gradle window, click Toggle Offline Mode (near settings icon).

enter image description here

It's a little bit confusing on the icon, anyway offline mode is enabled when the toggle button is highlighted. :)

Evertor answered 3/10, 2020 at 3:1 Comment(3)
Thanks a lot man, I really couldn't to figure out with it by myself.Zomba
this answer works!!!! ++UP to TOP... build time reduced from 37min to 3 min .10x speed up... fcking network checks.( installed And_Studio at 2022.10.31Gussi
It works!! Thanks a lot on Android studio preview on 2022.2.1 Canary 2. I also add org.gradle.parallel=true on setting.gradle and turn on File/Power save mode. Build time reduced from 15min to 1min.Roley
J
26

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
Jp answered 6/4, 2016 at 18:44 Comment(5)
You can set it directly from gradle.properties located under studio project.Clypeate
Yes also enable this option org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8Jp
Set MaxPermSize=1024mClypeate
Why are those properties not predefined?? Are there any downsides?Whitson
This option should only be used with decoupled projects. More details, visit gradle.org/docs/current/userguide/…Laritalariviere
F
17

Enabling Java 8 features caused deadly slow build

gradle

 jackOptions {
        enabled true
  }

  compileOptions {
    targetCompatibility 1.8
    sourceCompatibility 1.8
}

After deleting above lines, it builds in seconds.

There is issue Compiling with Jack takes very long time

Project Manager's Answer

We're aware that build times are an issue with Jack right now. We have improvements in the 2.4 Gradle plugin that should be a significant improvement for incremental builds.

As of now, latest Gradle version i can find is 2.3.0-beta4

Fluoric answered 17/2, 2017 at 6:54 Comment(1)
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 GoogleMerrile
S
16

Recommended Reading: How I save 5h/week on Gradle builds!

According to this excellent post you should try to optimized the following:

  • Gradle Daemon
  • Parallel Project Execution
  • Configure projects on demand
  • Modules are expensive...
Seadon answered 20/4, 2016 at 8:53 Comment(0)
L
7

@AndroidDev solution worked for me. I had included the whole lib in gradle.

compile 'com.google.android.gms:play-services:8.4.0'

Changing to solved the problem. From 4 min to 1 min.

compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.google.android.gms:play-services-analytics:8.4.0'

Thnx

Lap answered 3/2, 2016 at 20:13 Comment(0)
D
7

Check your Internet connection. If internet speed is very slow gradle build will also take long time to build. I check by change my wifi internet connection with another one good speed connection. Now build time is normal. Please try this before you go to other solutions. somebody will take wrong decision to reinstall or change settings. I hope it will help. Thanks and regards.

Drury answered 23/1, 2017 at 7:1 Comment(2)
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 thoughInterplay
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 dependencyKristelkristen
W
3

1) My build time severely increased after i added some new library dependencies to my gradle file, what turned out that i need to use multidex after that. And when i set up multidex correctly then my build times went up to 2-3 minutes. So if you want faster build times, avoid using multidex, and as far as possible, reduce the number of library dependencies.

2) Also you can try enabling "offline work" in android studio, like suggested here, that makes sense. This will not refetch libraries every time you make a build. Perhaps slow connection or proxy/vpn usage with "offline work" disabled may lead to slow build times.

3) google services - as mentioned here, dont use the whole bundle, use only the needed parts of it.

Witkin answered 17/10, 2016 at 11:27 Comment(0)
R
2

I was facing the same problem for a long time but I solved it by adjusting the settings in gradle.

Step 1:In Module app add dependency in BuildScript

buildscript {
 dependencies {
         classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
}
}

Step 2: Add dexOption and give the following heapSize

  dexOptions {
    incremental = true;
    preDexLibraries = false
    javaMaxHeapSize "4g"
}

Step 3: Add productFlavors

    productFlavors {
    dev {
        minSdkVersion 23
        applicationId = "com.Reading.home"
    }
    prod {
        minSdkVersion 15
        applicationId = "com.Reading.home" // you don't need it, but can be useful

    }
}

This should reduce your build time.

Retortion answered 12/2, 2016 at 6:50 Comment(0)
M
1

In the beta version of Android studio 2.0, they've rolled out a feature called Instant Run. It reduces the build time by a big factor. The core concept is, Android Studio only pushes the small changes made into an already running app.

It classifies the changes we make in our code to three parts: hot swap, warm swap and cold swap, based on how much load would the change would put on the build. After that, it simply swaps the code in the already running app and we get could see the changes running a few seconds. See their doc for more information.

Here you can download Android Studio 2.0 preview.

PS: There are a few issues with this feature. They've listed it out at the docs. Check it out before installation.

Maximin answered 9/3, 2016 at 19:41 Comment(0)
V
1

Check disk usage in Windows OS ...

The problem can be related to this, in my case I had Windows 8 OS consuming a 100% disk usage.

Especially the Windows Search service, can be the most consuming. Disable it with:

cmd (Admin)

net.exe stop "Windows search"

Check this link 4 Tips to Fix 100% Disk Usage & Improve Windows Performance

Vituperation answered 28/2, 2017 at 22:53 Comment(0)
E
1
  1. Go to File->Setting-->Gradle-->Set it to offline mode and check
Everyone answered 31/10, 2017 at 10:22 Comment(0)
B
1

I had issues like this especially when debugging actively through my phone; at times it took 27 minutes. I did the following things and take note of the explanation under each - one may work for you:

  1. Changed my gradle.properties file (under Gradle scripts if you have the project file view under Android option OR inside your project folder). I added this because my computer has some memory to spare - you can assign different values at the end depending on your computer specifications and android studio minimum requirements (Xmx8000m -XX:MaxPermSize=5000m) :

org.gradle.daemon=true

org.gradle.configureondemand=true

org.gradle.parallel=true

android.enableBuildCache=true

org.gradle.caching=true

org.gradle.jvmargs=-Xmx8000m -XX:MaxPermSize=5000m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

  1. This did not completely solve my issue in my case. Therefore I also did as others had suggested before - to make my builds process offline:

File -> Settings/Preferences -> Build, Execution, Deployment -> Gradle

Global Gradle Settings (at the bottom)

Mark the checkbox named: Offline Work.

  1. This reduced time substantially but it was erratic; at times took longer. Therefore I made some changes on Instant Run:

File -> Settings/Preferences -> Build, Execution, Deployment -> Instant Run

Checked : Enable Instant Run to hot swap code...

Checked: restart activity on code changes ...

  1. The move above was erratic also and therefore I sought to find out if the problem may be the processes/memory that ran directly on either my phone and computer. Here I freed up a little memory space in my phone and storage (which was at 98% utilized - down to 70%) and also on task manager (Windows), increased the priority of both Android Studio and Java.exe to High. Take this step cautiously; depends on your computer's memory.

  2. After all this my build time while debugging actively on my phone at times went down to 1 ~ 2 minutes but at times spiked. I decided to do a hack which surprised me by taking it down to seconds best yet on the same project that gave me 22 - 27 minutes was 12 seconds!:

Connect phone for debugging then click RUN

After it starts, unplug the phone - the build should continue faster and raise an error at the end indicating this : Session 'app': Error Installing APKs

Reconnect back the phone and click on RUN again...

ALTERNATIVELY

If the script/function/method I'm debugging is purely JAVA, not JAVA-android e.g. testing an API with JSONArrays/JSONObjects, I test my java functions/methods on Netbeans which can compile a single file and show the output faster then make necessary changes on my Android Studio files. This also saves me a lot of time.

EDIT

I tried creating a new android project in local storage and copied all my files from the previous project into the new one - java, res, manifest, gradle app and gradle project (with latest gradle classpath dependency). And now I can build on my phone in less than 15 seconds.

Birdcage answered 31/10, 2018 at 13:11 Comment(0)
G
1

In your build.gradle, if you have minifyEnabled for debug, remove it. I had it in my project and it took ~2-3 minutes to build. After looking at build progress, I found this as culprit, so deleting the commented line below, my issue was fixed.

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }//debug{
         //   minifyEnabled true
         //   proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}
Gastronome answered 11/12, 2018 at 17:58 Comment(0)
C
1

The second thing i did was Uninstall my Anti-Virus software (AVG Antivirus) {sounds crazy but, i had to}. it reduced gradle build time upto 40%

The first thing i did was enable offline mode (1. click on Gradle usually on the right side of the editor 2. click on the connection button to toggle) it reduced the gradle build time for upto 20%

so my Gradle build time was reduced for upto 60% by doing these two things

Cleotildeclepe answered 19/12, 2020 at 9:7 Comment(0)
A
0

I had the same issue in kotlin. Updating the outdated kotlin runtime solved it for me

Austronesian answered 27/12, 2017 at 8:58 Comment(0)
S
0

You could try the tips in this post Why your Android Studio takes forever to build - Part 2 One of the tips recommmends "Enable offline mode" among other things.

Sammie answered 27/2, 2019 at 23:19 Comment(0)
P
0

I had the same problem, even the gradle build ran for 8 hours and i was worried. But later on i changed the compile sdk version and minimum sdk version in build.gradle file like this.

Older:

android {
compileSdkVersion 25
buildToolsVersion "29.0.2"

defaultConfig {
    applicationId "com.uwebtechnology.salahadmin"
    minSdkVersion 9
    targetSdkVersion 25
}

New (Updated):

android 
{
  compileSdkVersion 28
   buildToolsVersion "25.0.2"

 defaultConfig {
    applicationId "com.uwebtechnology.salahadmin"
    minSdkVersion 15
    targetSdkVersion 28
 }
Philologian answered 26/4, 2020 at 11:39 Comment(0)
K
0

In my case, after days of research and trying everything, I had to downgrade the Kotlin version! I was using the latest 1.6.20 (04 / 2022), I tried the 1.6.0 version it was the same, I had to downgrade to 1.5.32 from never build (I waited 26 hours and still running) to few sec....

Kaceykachina answered 17/4, 2022 at 15:24 Comment(0)
A
0

I sometimes face this problem. This solution is a problem solver for me. So try in Once. first, be sure of your proxy connection. second, Remove cash and indexes for all projects. For this, you need to proceed through the following address. file -> invalidate Cashes then tick all optional items then select Invalidate and Restart Button then restart android studio and rebuild your app. I hope it is useful.

Asymptote answered 11/2, 2023 at 10:36 Comment(0)
E
-2

I had a similar issue on my computer. Windows Defender was blocking some part of Gradle Building. I've disabled it, worked fine after that.

Eaglewood answered 29/6, 2016 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.