Android studio Gradle build speed up

2019-01-01 14:42发布

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?

18条回答
若你有天会懂
2楼-- · 2019-01-01 15:05

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

查看更多
裙下三千臣
3楼-- · 2019-01-01 15:07

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'
}
查看更多
与风俱净
4楼-- · 2019-01-01 15:08

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/

查看更多
心情的温度
5楼-- · 2019-01-01 15:08

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.

查看更多
闭嘴吧你
6楼-- · 2019-01-01 15:12

Add a build.gradle file:

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

I hope it helps.

查看更多
柔情千种
7楼-- · 2019-01-01 15:13
  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

查看更多
登录 后发表回答