android – Speed up gradle build in multidex application-ThrowExceptions

Exception or error:

My application has a bunch of librarys that are essential that is why I was forced to use multidex support library and it works nicely. But where the problem shows is in the gradle buid speed. It takes on average 2minutes to build and when I am developing and testing this is quite annoying.

Is there a way to speed up my debug builds?

How to solve:

You can speed-up your development builds by specifying the minimum SDK version = 21.
Official documentation includes a whole section about that.

Example (from documentation):

android {
    productFlavors {
        // Define separate dev and prod product flavors.
        dev {
            // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
            // to pre-dex each module and produce an APK that can be tested on
            // Android Lollipop without time consuming dex merging processes.
            minSdkVersion 21
        }
        prod {
            // The actual minSdkVersion for the application.
            minSdkVersion 14
        }
    }
          ...
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                                                 'proguard-rules.pro'
        }
    }
}
dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

Once you added the product flavors, you can use the devDebug task (instead of default debug task) for your development builds:
– from command line: run ./gradlew installDevDebug
– from Android Studio: open Build Variants window and select the devDebug build variant.

You should, of course, work against a device whose SDK >= 21.


There’s also a solution for those who don’t want to use flavors. As suggested in this gist, dynamically calculate the minSdkVersion value:

int minSdk = hasProperty('devMinSdk') ? devMinSdk.toInteger() : 14
apply plugin: 'com.android.application'

android {
    ...
    defaultConfig {
        minSdkVersion minSdk
        ...
    }
}

In this example, we’re checking if devMinSdk property defined, and if true – we’re using it. Otherwise, we default to 14.

How do we pass devMinSdk value to build script? Two options:

Using command line:

./gradlew installDebug -PdevMinSdk=21

Using Android Studio preferences:

Go to Preferences (Settings on Windows) -> Build, Execution, Deployment -> Compiler -> put -PdevMinSdk=21 in Command-line Options text box.

Android Studio compiler options

###

Recently build cache was introduced by team working on Android Gradle plugin. You can enable it by adding android.enableBuildCache=true to gradle.properties.

More info here
http://tools.android.com/tech-docs/build-cache

For me it increased incremental build times by ~30 seconds.

It doesn’t work with legacy multidex (com.android.support:multidex) introduced as part of support library, so it’s suitable only if your minSDK >= 21. You can set it only for your your development builds and do release builds with minSDK < 21.

It also works without multidexing enabled.

###

Android Studio 1.3 (currently in Preview 3) is using a new build system which improved gradle build time (really, like 10-30x faster).

More information in the Live Session at Google I/O 2015

###

Multidexing uses more memory. As you get closer to your max heap size in Java you’ll find Java spends more time doing GC than it does doing any real work, this can slow things down a lot.

I’d strongly recommend increasing the max heap size when using multidex. Add the following to the android closure in your build.gradle file to make the max heap size 4GB (Make it smaller if you wish):

dexOptions {
    javaMaxHeapSize "4g"
}

###

Changing MinSdk to 21 made everything back to normal for me.Now everything compiles in like 6s

###

This is no longer needed with the latest Android Studio 3.0

###

Go in setting , search compiler , type “–offline” in Command line options and than compile.

Leave a Reply

Your email address will not be published. Required fields are marked *