Search code examples
gradleandroid-build

Gradle build slow on transformClassesWithDexForDebug


Building my Android app takes about 90 seconds ("fast"), up to 3 minutes for each update to my code. It's a complete waste of time as it really and I assume a solution must be within reach. I tried investigating the issue and found different blog-posts and SO answers with suggestions, most of which I've tried.

  • I have the gradle.properties file with org.gradle.deamon=true
  • I run on Android Studio with Gradle Prefence to do offline work (improved, but still slow)
  • I run on command line (which is faster, but still slow)
  • In build.gradle, defaultConfig, I have multiDexEnabled set to false
  • In build.gradle, dexOptions, I have preDexLibraries set to false
  • In gradle-wrapper.properties I fetch a recent gradle version (2.8) (the significant speed changes happened on 2.4)

The process that seems to take long, about 85% of total build time is :app:transformClassesWithDexForDebug

What is that process actually doing? I can find people who have crashes on it, but it works fine for me, except for the fact that it takes a lot of time. And do I need it, since I don't really need Dex at this point?

Also, I have 13 dependencies and 3 testCompile dependencies. I already point to specific play packages, so I'm not compiling stuff I don't need. If I understand things correctly, gradle is building all those libraries each project build as well. If that is correct, is there a way to skip that? Can I build them myself as wrapped-up libraries and include them without the need for them to be processed each time? That might make me lose some flexibility for future changes to dependencies, but at this point I feel like I'm losing over an hour a day easily on waiting for gradle. I'm not sure if flexibility is worth that much to me.

I'm looking forward to get any pointers on how I can improve my build process. Thank you in advance.


Solution

  • 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