Java – Closing some code to speed up build time (Gradle)

Closing some code to speed up build time (Gradle)… here is a solution to the problem.

Closing some code to speed up build time (Gradle)

I have an Android project that has grown over time, and gradle build times have increased as it has grown in size.

It is tolerable below the 65k limit – about 14s.
Now using Multidex requires 36s.

So my question is – is there any way to “turn off” the unused piece of code so that it goes back below the 65k limit?

For example, there are thousands of ways to turn off the Amazon S3 SDK that comes with Gradle.

I know you can strip the code using a obfuscator, but that only increases build time.

When I open the section that uses it, I’m glad it crashes when run, just wanted to make the test faster.

When I removed Amazon from Gradle Imports, I apparently got this:
Error:(24, 26) Error: Package com.amazonaws.auth does not exist

Is there a way to somehow ignore the error? I know that in Picasso it has a runtime check to see if you have OkHttp, and if not – use a standard network.

static Downloader createDefaultDownloader(Context context) {
    if (SDK_INT >= GINGERBREAD) {
       try {
         Class.forName("com.squareup.okhttp.OkHttpClient");
         return OkHttpLoaderCreator.create(context);
       } catch (ClassNotFoundException ignored) {}
    }
    return new UrlConnectionDownloader(context);
}

Can I do something like this? Or is it some other way?

Solution

The only realistic way (as far as I know) is to refactor your project so that your package is split into separate modules. So you’ll have separate gradle build files for each module, but just recompile them when you touch each module. For example, you can have a data access package and a UI package. This seems like a natural split.

I know this is a disappointing answer, but the problem you’re prompting is that your build dependencies require all those extra unnecessary libraries and method calls: not your code using them.

The only other tip I can give you is that the Google Play API toolkit has tens of thousands of method calls. If you can only use the part you’re using, you’re more likely to be below the 65k limit.

Related Problems and Solutions