Home » Android » Android Studio – Gradle incremental build

Android Studio – Gradle incremental build

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

I do not have a direct answer how to solve the problem with Gradle, but I have a suggestion that might help you increase the speed of your builds.

Recently I run into the same problem: our build took nearly 4 minutes. I tried different build options but didn’t come to noticeable profit.

After some research, I run into Jrebel for Android. It is something like “Instant run” on steroids. It supports many types of changes (UI, methods, fields, etc) and updates application in real time without recreating activity (therefore, saving state). Usually, it takes ten-thirty seconds to apply any kind of change.

The tool is not free, but they provide different pricing options and trial account, so you can try it. It works as usual plugin for Android Studio or Eclipse and installation is very easy.

Update

Now they have even free version: link to post

Answer:

I tried this option today (13 Nov 2017) and got the following build warning:

Warning:The android.dexOptions.incremental property is deprecated and it has no effect on the build process.

I then googled for the warning message and the answer confirmed that since a newer Gradle release, the entry is deprecated. See android.dexOptions.incremental property is deprecated