239

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  
MMG
  • 2,962
  • 5
  • 8
  • 35
AndroidDev
  • 4,071
  • 5
  • 28
  • 55

22 Answers22

471

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

Uddhav Gautam
  • 6,052
  • 3
  • 39
  • 54
B K
  • 9,227
  • 4
  • 34
  • 41
  • 11
    Worked great. From 12 mins to 5 seconds – Ramesh Mar 17 '16 at 18:15
  • 20
    won't this configuration stop fetching new libraries when we add them to build.gradle ? – Dany Y Mar 29 '16 at 13:59
  • 49
    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 – B K Mar 29 '16 at 18:47
  • 17
    Does it mean that all the time the issue was internet speed instead of processor speed? –  Sep 16 '16 at 12:08
  • Amazed!! minimize time from 1 min to 2 secs on SSD. – meyasir Oct 22 '18 at 08:23
  • 1
    Woah! Went from over 15 minutes to 1 minute :O – Cardinal System Sep 16 '19 at 02:27
  • 1
    unfortunately this did not have any effect on my system. – drlolly Oct 09 '19 at 13:22
  • 11
    is it only me or there's no Offline Work option anymore in Android Studio 3.6.2? – Fugogugo Jul 02 '20 at 08:23
  • greaaaat....!! after 1st compilation now when i switch to offline, everything was blasttt....! great :D – gumuruh Jul 16 '20 at 13:58
  • 3
    @Fugogugo - take a look at these posts: https://stackoverflow.com/questions/57797772/cannot-enable-gradles-offline-mode-on-android-studio-3-6#:~:text=From%20the%20Android%20Studio%203.6,button%20in%20the%20Gradle%20panel.. and https://stackoverflow.com/questions/60475046/disable-gradle-offline-mode-in-android-studio-3-6 – Peter Sep 16 '20 at 10:19
153

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

AndroidDev
  • 20,063
  • 26
  • 131
  • 216
  • 12
    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!! – Sauron Mar 08 '16 at 23:43
  • Thanks a lot,this was my main problem too. Now everything working great. – alnaji Apr 17 '16 at 21:29
  • 1
    Works great. Use this link to find your play services library name: https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project – PC. May 07 '16 at 05:38
  • You deserved a tick... You have solved my problem. The problem is on google play services... Thanks :) – Karue Benson Karue May 29 '16 at 21:05
  • 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 Nov 13 '16 at 19:27
  • @AndroidDev - this helps in 2018!!!. Do you know if this is a bug or this is how gradle build should work? – Amod Gokhale Mar 05 '18 at 15:07
57

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

Shashanth
  • 4,151
  • 7
  • 32
  • 46
39

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

T.Todua
  • 44,747
  • 17
  • 195
  • 185
AndroidDev
  • 4,071
  • 5
  • 28
  • 55
  • 3
    How to remove the proxy server ? – Thiago Aug 28 '15 at 02:29
  • 2
    On Windows File > Settings > Appearance & Behavior > System settings > HTTP Proxy – AndroidDev Aug 28 '15 at 07:02
  • 2
    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 – user3098756 Nov 05 '15 at 14:55
  • 37
    AS is complete joke and bs. I am just tired of this nonsense. – The_Martian Apr 12 '16 at 05:18
  • 9
    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 – Josh Aug 03 '16 at 06:33
  • 1
    @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. – magritte Nov 13 '16 at 21:01
  • 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 – Josh Nov 21 '16 at 12:44
  • 1
    errr would it really hurt to just put an error message stating that? – Neon Warge Dec 26 '16 at 07:17
  • Why does it need to connect to a server to build something? – Jonathan. May 04 '18 at 06:12
  • As guys are getting answers...am 11 minutes in waiting for game to finish – David Innocent Dec 30 '19 at 22:41
  • As of Android 4.1: Removing proxy: `File > Settings > Appearance & Behavior > System settings > HTTP Proxy` > `No proxy` – Prajwal Waingankar Oct 16 '20 at 10:30
24

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
user779370
  • 1,534
  • 1
  • 10
  • 8
  • 5
    You can set it directly from gradle.properties located under studio project. – B K Apr 06 '16 at 18:49
  • 3
    Yes also enable this option org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 – user779370 Apr 06 '16 at 18:51
  • 2
    Set MaxPermSize=1024m – B K Apr 06 '16 at 19:03
  • Why are those properties not predefined?? Are there any downsides? – oli Nov 30 '16 at 19:07
  • 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 – javad Dec 09 '16 at 09:54
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

tchelidze
  • 7,334
  • 1
  • 25
  • 44
  • 4
    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 – Karol Żygłowicz Feb 21 '17 at 12:28
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...
Oded Regev
  • 3,235
  • 2
  • 34
  • 49
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

KlevisGjN
  • 595
  • 2
  • 8
  • 15
6

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.

  • 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 – Mohd.Zafranudin Oct 13 '18 at 13:25
5

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. :)

Amal Dev S I
  • 538
  • 10
  • 15
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.

lxknvlk
  • 2,230
  • 20
  • 28
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.

Binesh Kumar
  • 2,605
  • 1
  • 13
  • 21
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.

Akeshwar Jha
  • 4,190
  • 5
  • 42
  • 85
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

dianakarenms
  • 2,220
  • 1
  • 18
  • 22
1
  1. Go to File->Setting-->Gradle-->Set it to offline mode and check
vikas khot
  • 31
  • 2
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.

Firsake
  • 31
  • 3
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

Nasib
  • 445
  • 5
  • 15
0

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

Rami Amro Ahmed
  • 389
  • 1
  • 5
  • 9
0

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'}
Mukesh
  • 94
  • 9
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.

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
 }
Pir Fahim Shah
  • 9,541
  • 1
  • 73
  • 71
-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.

rickdroio
  • 385
  • 2
  • 6