Search code examples
androidandroid-studiogradleproguardandroid-proguard

Android Studio - Gradle incremental build


In the last few days I have been trying to improve the build time for our project with no luck. I don't mind having to wait 1m40s for a clean build, but if I insert one line in a single java file, I get the same build time as in a clean build. I must be doing something wrong but I simply can't find any documentation or stack overflow question that points me in the right direction. I managed to gather a bunch of information that I will centralize here in the hope that someone with a better understanding of the topic explain me why I get these abysmal build times.

Here it goes:

  • I understand that this option in my build.gradle file is what I seek.

    android {
        dexOptions {
            incremental true
        }
    ...
    

    Glorious incremental build. However, this doesn't work for me apparently because my project uses the multidex feature for retrocompatibility with android versions before Lollipop.

  • Proguard to the rescue! With Proguard I am able to minify my executable and remove all the unused methods. I got a couple of errors with proguard minify but was able to set it properly and get it working. Great! Dex generation takes about 20s for every build. Unfortunately, nothing is for free, and the proguard gradle task takes about 2m50s to run. Even worse than my initial case.

Is there a solution to this problem?


Solution

  • Finally found a solution. It was in the official docs all along, I just needed a deeper understanding of the build process to understand what was being said.

    Incremental builds and multidex are indeed incompatible options. However, for sdk versions 21+ incremental builds are possible for multidex apks by rebuilding only the affected dex files. Further information can be found here:

    http://developer.android.com/tools/building/multidex.html