Gradle build slow on transformClassesWithDexForDebug

Edit: At this point, I recommend running Android Studio 2.x side by side with your 1.5 installation. You get access to instant-run, which really helps as well as all the updated tools. If you are staying on 1.5 read on...

I have managed to speed up Android Studio 1.5 debug builds from 2 minutes to 30 seconds. This might not work with your command-line execution, but might be faster.

Using this config, your first IDE build takes the same amount of time, but Incremental builds are faster, even if you modify classes. You lose some gain if you modify attached libraries.

Step 1. (If you're fortunate enough to target minSdkVersion of >= 21 already, skip this.)

@vanomart 's answer of having a minSdkVersion debug flavour of >= 21 is not wrong, but the only part necessary is adding the following to module (app) build.gradle and ensuring you target dev while debugging in your Build Variants tab:

android {
    [...]
    productFlavors {
        dev {
            minSdkVersion 21 //The important bit.
        }
        prod {
            minSdkVersion 15 //Example. Set to your real requirement.
        }
    }

Step 2. Pre-dexing libs.

In your module (app) build.gradle set the following config. This is faster for IDE builds, not so much for builder-servers who start from scratch every build.

android {
    [...]
    dexOptions {
        preDexLibraries true
        javaMaxHeapSize "2g" // Use gig increments depending on needs
    }

Source, doing (partly) inverse of "Improving Build Server performance": http://tools.android.com/tech-docs/new-build-system/tips#TOC-Improving-Build-Server-performance

Step 3. Make sure you're using latest buildToolsVersion in module (app) build.gradle.

android {
    buildToolsVersion "23.0.2"
    [...]

"... update the build tools version in all your modules to the latest (23.0.2). [...] it will use a new faster version of dex, which helps both instant run and a full build be a bit faster."

Source: http://tools.android.com/tech-docs/instant-run

Step 4. Use latest Gradle build tools

In your project build.gradle, set to latest (currently 2.0.0-alpha6)

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

Updates list: http://tools.android.com/tech-docs/new-build-system

Step 5. Use latest Gradle wrapper. Modify gradle-wrapper.properties, update this line to use 2.10:

distributionUrl=https\://downloads.gradle.org/distributions/gradle-2.10-all.zip
#Alternative url if the above does not work:
#distributionUrl=https://services.gradle.org/distributions/gradle-2.10-all.zip

In Android Studio preferences, ensure you have "Use default Gradle wrapper" selected. I recommend restarting Android Studio to ensure the Gradle daemon restarts.

"In many cases, Gradle 2.9 is much faster than Gradle 2.8 when performing incremental builds."

Source: docs.gradle.org/current/release-notes


Upgrading to Android Studio 2.1 and the Android Gradle Plugin v2.1.0 has largely fixed this problem for me. After installing the updated IDE, you should be prompted to update your Gradle plugin as well. You'll know you have the right version if your root build.gradle file has the following line:

classpath 'com.android.tools.build:gradle:2.1.0'

IMPORTANT: In addition to upgrading, you also need to increase the amount of memory allocated to the Gradle daemon to 2048mb, so it can perform this expensive dex-ing step in process. To do so, add the following to your root gradle.properties file:

org.gradle.jvmargs = -Xmx2048m

I had similarly slow build times as experienced in the question above, but after upgrading my build speeds increased dramatically. See the release notes for the Android Gradle Plugin v2.1.0 here for more info:

http://developer.android.com/tools/revisions/gradle-plugin.html