167

Since the last update (Build from june 25) any changes in the Android studio Gradle is painfully slow. And it also seems to autotrack changes when you edit the file and recompile on keyup.

Each change takes several minutes on my i5.

Any idea how I can speed up my Gradle changes?

guo
  • 9,674
  • 9
  • 41
  • 79
Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
  • 1
    possible duplicate of [Building and running app via Gradle and Android Studio is slower than via Eclipse](http://stackoverflow.com/questions/16775197/building-and-running-app-via-gradle-and-android-studio-is-slower-than-via-eclips) – Dave Jarvis Jan 05 '15 at 07:57
  • 1
    For those who take the time to read the answer and the question, this is not a duplicate question, as suggested by @Dave Jarvis. But it is highly related to an old beta version of AS and probably has little to no bearing on current versions. – Einar Sundgren Jan 06 '15 at 09:35
  • See this http://stackoverflow.com/a/36826562/1770868 – Ahmad Aghazadeh Dec 04 '16 at 17:56
  • See this https://stackoverflow.com/a/54852468/7137330 – Jay Feb 24 '19 at 13:40

24 Answers24

101

Definitely makes a difference: How To… Speed up Gradle build time

Just 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
Czechnology
  • 14,832
  • 10
  • 62
  • 88
Francois
  • 1,555
  • 2
  • 13
  • 8
  • 2
    Good answer. For further and detailed reading, check [this](http://stackoverflow.com/a/19500539/1276636) SO answer. – Sufian Jan 07 '15 at 06:32
  • 13
    From your link: "Note: This does only affect console builds. Android Studio always uses a Gradle daemon (and depending on your settings some other optimizations)." – ChiefTwoPencils Feb 01 '15 at 23:57
  • 2
    I have **.gradle** directory with **gradle.properties** file. But still take it much time. Any suggestion – CoDe Oct 26 '15 at 11:44
  • 5
    Doesn't make any difference – Vlado Pandžić Jan 27 '16 at 14:24
  • And restart Android Studio after make change. – Iman Marashi Dec 10 '16 at 15:59
  • almost end of 2017, same poor performance, taking 10-15 minutes for each action. Using i5 with 4GB RAM, no other applications are working. Any better solution. Using Android Studio 2.2. If i switch to AS 3+ will the performance improve? – Uday Dec 07 '17 at 16:34
  • @Uday In 2017-2018 it's OK to have 4Gb RAM when it is your MOBILE PHONE) As for me it's impossible to work comfortable on 4Gb at all. – Yazon2006 Nov 20 '18 at 15:57
  • does it have any unwanted side effects, can it be used in production – DragonFire Jun 03 '20 at 00:44
  • The side effect is that on the machine that builds binaries, the gradle process is kept alive, which can waste a little memory on the build machine. The resulting binaries are identical thoughl – Mooing Duck Jun 26 '23 at 02:50
80

After change this settings my compile time 10 mins reduced to 10 secs.

Step 1:

Settings(ctrl+Alt+S) ->

Build,Execution,Deployment ->

Compiler ->

type "--offline" in command-line Options box.

Step 2:

check the “Compile independent modules in parallel” checkbox.

& click Apply -> OK

enter image description here

Step 3: In your gradle.properties file -> Add following lines

org.gradle.jvmargs=-Xmx2048M -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.daemon=true

Update:

If you are using Android studio 2.0 or above try the Instant Run

Settings → Build, Execution, Deployment → Instant Run → Enable Instant Run.

More info about Instant Run - https://developer.android.com/studio/run/index.html#instant-run

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
  • 7
    I tried this and it really speed up my build substantially. I know that the option will now cause it to build offline, is there any disadvantages to building this offline? – Simon Aug 01 '15 at 07:46
  • 6
    @Simon - The disadvantage is you will not be able to pull down the latest versions of the dependencies identified in your build.gradle file. It runs faster because it uses a cached snapshot of those imported libraries. For some devs, stability is paramount but going offline too long may screw them over when they finally go online and those dependencies have evolved thus leaving the said project behind. – Mark Lapasa Sep 04 '15 at 04:00
  • 1
    Thanks bro this one worked fine for me and my gradle build speed is increased – Android_programmer_office Oct 06 '15 at 06:30
  • @Simon, when you start developing, you can simply set the settings above, then once stop, turn them off just as the night ends, running the app one last time so that everything is still in sync. Or would there be an issue with this? – Sauron Feb 25 '16 at 01:08
  • 1
    @Sauron, I think if you did that, the dependencies would all be updated at the end of the day, which might mean changes in your code base as methods in your dependencies could have changed, been deprecated etc. Best to keep the offline checkbox unticked unless your build time is horrible – Simon Feb 25 '16 at 04:51
  • @Miron can you share your previous compile time & current compile time(after change this settings)? – Ranjithkumar May 09 '16 at 04:39
  • 2
    it decreased from 33 seconds to 5 seconds :)) – Miron May 09 '16 at 04:55
  • but as everything has a cost to pay. you may not be able to compile libraries available online. – Faisal Naseer Jul 30 '16 at 06:28
65

I was able to reduce my gradle build from 43 seconds down to 25 seconds on my old core2duo laptop (running linux mint) by adding the following to the gradle.properties file in android studio

org.gradle.parallel=true
org.gradle.daemon=true

source on why the daemon setting makes builds faster: https://www.timroes.de/2013/09/12/speed-up-gradle/

Sam
  • 2,427
  • 2
  • 23
  • 26
41

The dev are working on it. Like I posted in this answer the fastest solution right now is to use gradle from the command line and you should switch to binary libs for all modules you do not develop. On g+ there is a discussion with the developers about it.

Community
  • 1
  • 1
rivare
  • 817
  • 8
  • 6
  • I should obviously have joined [the tool discussion group](https://plus.google.com/communities/114791428968349268860) earlier. – Einar Sundgren Jun 26 '13 at 16:15
14

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

And check this options in Android Studio

enter image description here

enter image description here

Biswajit Karmakar
  • 9,799
  • 4
  • 39
  • 41
7

There is a newer version of gradle (ver 2.4).

You can set this for your project(s) by opening up 'Project Structure' dialog from File menu,

Project Structure -> Project -> Gradle version

and set it to '2.4'.
You can read more about boosting performance at this link.

Mr Robot
  • 1,747
  • 6
  • 35
  • 67
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28
7

This is what I did and my gradle build speed improved dramatically! from 1 min to 20sec for the first build and succeeding builds became from 40 sec to 5 sec.

In the gradle.properties file Add this:

org.gradle.jvmargs=-Xmx8192M -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

In the Command Line Arguments via Go to File > Other Settings> default Settings >Build, Execution, Deploy> Complier and add the following arguments to Command Line Arguments

Add this:

--debug --stacktrace -a, --no-rebuild -q, --quiet --offline

See image here

mangu23
  • 884
  • 9
  • 13
6

With Android Studio 2.1 you can enable "Dex In Process" for faster app builds.

enter image description here

You can get more info about it here: https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e#.vijksflyn

Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
6

Try to avoid using a Mac/PC that has only 8 GB of RAM when doing Android development. As soon as you launch even 1 emulator (Genymotion or otherwise), your build times become extremely slow in Android Studio with gradle builds. This happens even if you make a simple one-line change to 1 source file.

Closing the emulator and using a real device helps a lot, but of course this is very limiting and less flexible. Reducing the RAM usage setting of the emulator can help, but the best way is to ensure your laptop has at least 12-16 GB of RAM.

Update (June 2017): There are now several good medium.com articles that explain how to speed up Android Studio gradle builds in detail, and it even works on 8 GB machines:

The summarised consensus is:

Create a gradle.properties file (either global at ~/.gradle/gradle.properties or local to project), and add the following lines:

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
5
dexOptions {
    incremental true
    javaMaxHeapSize "4g"
}
AnupamChugh
  • 1,779
  • 1
  • 25
  • 36
4
  1. Enable Offline Work

  2. Improve Gradle Performance by adding following code in gradle.properties

org.gradle.daemon=true
org.gradle.parallel=true

Step by step guide:http://www.viralandroid.com/2015/08/how-to-make-android-studio-fast.html

Sajan Rana
  • 803
  • 7
  • 9
  • Let's keep these answers varied and focus on the particulars of the OP question. re: http://stackoverflow.com/a/34828203 ... http://stackoverflow.com/a/34827929 ... http://stackoverflow.com/a/34827858 ... http://stackoverflow.com/a/34827827 ... http://stackoverflow.com/a/34827788 – Drew Jan 16 '16 at 14:46
3

Acording to this page of the Android Team of Wikimedia Apps, a good way of optimize Gradle builds is adding this lines to your ~/.gradle/gradle.properties

org.gradle.daemon=true                                                          
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx2048M

For those who do not have the file there are two ways to do it:

  1. Add the file locally in your project by creating a file called gradle.properties in the project root or,

  2. You can set them globally for all your projects by creating the same file in your home directory (%UserProfile%.gradle on Windows, ~/.gradle on Linux and Mac OS X)

    It is a good practice to set the properties in your home directory, rather than on a project level.

Community
  • 1
  • 1
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
2

You can also use command line for better performance.You can use the command ./gradlew <task name> from inside the root folder of your project from linux or use gradlew.bat file like gradlew <task name>.when you first run one of the commands above for a given Gradle version, it will download the corresponding Gradle distribution and use it to execute the build.

When importing a Gradle project via its wrapper, your IDE may ask to use the Gradle 'all' distribution. This is perfectly fine and helps the IDE provide code completion for the build files. Not only does this mean that you don’t have to manually install Gradle yourself, but you are also sure to use the version of Gradle that the build is designed for. This makes your historical builds more reliable. for more info refer Executing a build with the Wrapper

khakishoiab
  • 9,673
  • 2
  • 16
  • 22
2

For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.

To do this set
org.gradle.jvmargs=-Xmx2048M
in the project gradle.properties.

Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
2

Add this to your gradle.properties file

org.gradle.daemon=true                                                          
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx2048M
Jay
  • 674
  • 11
  • 13
1

Add a build.gradle file:

android {
...
dexOptions {
        javaMaxHeapSize "4g" //specify the heap size for the dex process
    }
...
}

I hope it helps.

Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
ali ozkara
  • 5,425
  • 2
  • 27
  • 24
1

I’m running a 5th gen i7 with Windows 10 and a 1TB Solid State. I compressed the Android Studio Projects folder and got about an 80% boost. Hope this helps.

I then combined it with the above solutions ie (org.gradle.parallel=true, org.gradle.daemon=true). The performance boost was quite impressive.

Additionally:

All of the above answers are totally correct but I must state as an experience Android developer (of 4 and a half years) that: No Android/Gradle developer should be working on a machine with a spinner drive, you need to fork out for a Solid State. We all hit that play button in the IDE 100s of times per day. When I went from a spinner to SSD (post Gradle), my speed and efficiency was literally 2 – 4 times faster and I promise you I’m NOT exaggerating here.

Now I’m not talking about having a machine with a small SSD and a big spinner, I’m talking about 1 big SSD. If you already have a machine with a small SSD and a big spinner you can upgrade the small spinner to say a 500GB SSD and set the SSD as your main OS drive with your developer tools installed on it.

So if you’re working in a fast paced environment please show this post to your boss. A decent 1TB SSD will set you back about £300 (including VAT), or about £160 for a 500GB SSD. Depending on if you are a junior or senior Android developer the drive will pay for itself (in wages expenses) in 1 – 2 working weeks, or about 2 and a half to 5 working days if you invest in a smaller; say 500GB SSD.

A lot of developers may argue that this is not the case, but it is the case for Gradle, as the Gradle system is very hard on the direct disk access. If you work with .NET/C#/VB Net or other development tools you won’t notice much difference but the difference in Gradle is HUGE. If you act on this post I promise you, you won’t be disappointed. Personally I’m using fifth gen i7 with 8GB RAM which originally came with a 1TB Spinner and I upgraded it to a Samsung SSD 840 EVO 1TB and I’ve never looked back since. I bought mine from: https://www.aria.co.uk.

Hope this helps. Also I must state that this is NOT a commercially motivated post, I’m just recommending Aria as I’ve used them many times before and they’ve always been reliable.

user2288580
  • 2,210
  • 23
  • 16
1

few commands we can add to the gradle.properties file:

org.gradle.configureondemand=true - This command will tell gradle to only build the projects that it really needs to build. Use Daemon — org.gradle.daemon=true - Daemon keeps the instance of the gradle up and running in the background even after your build finishes. This will remove the time required to initialize the gradle and decrease your build timing significantly.

org.gradle.parallel=true - Allow gradle to build your project in parallel. If you have multiple modules in you project, then by enabling this, gradle can run build operations for independent modules parallelly.

Increase Heap Size — org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 - Since android studio 2.0, gradle uses dex in the process to decrease the build timings for the project. Generally, while building the applications, multiple dx processes runs on different VM instances. But starting from the Android Studio 2.0, all these dx processes runs in the single VM and that VM is also shared with the gradle. This decreases the build time significantly as all the dex process runs on the same VM instances. But this requires larger memory to accommodate all the dex processes and gradle. That means you need to increase the heap size required by the gradle daemon. By default, the heap size for the daemon is about 1GB.

Ensure that dynamic dependency is not used. i.e. do not use implementation 'com.android.support:appcompat-v7:27.0.+'. This command means gradle will go online and check for the latest version every time it builds the app. Instead use fixed versions i.e. 'com.android.support:appcompat-v7:27.0.2'

Ashish Kumar
  • 1,088
  • 8
  • 17
1

Open gradle.properties from android folder and uncomment highlighted lines and provide memory values as per your machine configuration. I have 8gb ram on my machine so I gave maximum 4096mb and 1024mb respectively.

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
//Uncomment below line for providing your system specific configuration
#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
 //Uncomment below line to allow parallel process execution.
 #org.gradle.parallel=true

My build time was reduced to half after this. Happy building!!

Yuri
  • 4,254
  • 1
  • 29
  • 46
Rajesh Sharma
  • 161
  • 1
  • 4
0

It often happens when you enabled multidex in you project. This can potentially slow your development process!! According doc:

multidex configuration requires significantly increased build processing time because the build system must make complex decisions about which classes must be included in the primary DEX file and which classes can be included in secondary DEX files. This means that incremental builds using multidex typically take longer and can potentially slow your development process.

but you can optimize this:

To mitigate longer incremental build times, you should use pre-dexing to reuse multidex output between builds.

If you're using Android Studio 2.3 and higher, the IDE automatically uses this feature when deploying your app to a device running Android 5.0 (API level 21) or higher.

So you need to set the minSdkVersion to 21 or higher!

But if you production version need to support minSdkVersion lower than 21, for example 19

you can use productFlavors to set minSdkVersion 21 for you dev version:

    android {
    defaultConfig {
        ...
        multiDexEnabled true
        // The default minimum API level you want to support.
        minSdkVersion 15
    }
    productFlavors {
        // Includes settings you want to keep only while developing your app.
        dev{
            //the IDE automatically uses  pre-dexing feature to mitigate longer incremental when deploying your app to a device running Android 5.0 !
            minSdkVersion 21
        }
        prod {

        }
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile 'com.android.support:multidex:1.0.3'
}
Serg Burlaka
  • 2,351
  • 24
  • 35
0

I do prefer building from command line for better build times. If your app code base is large and you have multiple modules then you can try Local AAR approach as described here, it will give you a big boost in Android Studio performance & gradle build times. Its compatible with command line builds as well

https://blog.gojekengineering.com/how-we-improved-performance-and-build-times-in-android-studio-306028166b79

Demo project with integration instructions can be found here: https://github.com/akhgupta/AndroidLocalMavenRepoAARDemo

Akhil
  • 6,667
  • 4
  • 31
  • 61
0

Right click on Drawable folder and convert png/jpg images to webp format it improved the gradle significantly

1234567
  • 2,226
  • 4
  • 24
  • 69
0

From the android developer doc:

Configure JVM garbage collector. It reduced the build time to 2 to 4 seconds

gradle.properties file

org.gradle.jvmargs=-Xmx1536m -XX:+UseParallelGC

Refer to this Developer doc

Jack
  • 1,825
  • 3
  • 26
  • 43
0

Follow Android Studio and Gradle performance guidelines

Overall

  • Always keep Gradle version up-to-date to receive new performance improvements
  • Modularize your project as much as possible to benefit from Gradle parallel work
  • Know Gradle properties and understand what they do
Kirill Kitten
  • 1,759
  • 1
  • 4
  • 5