Android Quick Tips #3 — Gradle

Since we heard that our previous Android Quick Tips added some value to a bunch of developers, we've decided to continue this series with a variety of topics. This week we'll look at the Gradle build system.

Android Quick Tips #1 Series Overview

Speeding Up Your Builds

Of course, all of us would love having faster builds. Especially in complex projects it can take quite some time until we finally have the compiled .apk. But you don't always have to buy a more expensive machine. Here are two small tips to make your build faster:

Activate Gradle Daemon

If you're working as an Android developer regularly, you just have to activate the Gradle daemon. If you haven't heard about it, let's just quote from the official docs for an explanation:

The Gradle Daemon is a background process that does the heavy lifting of running builds, then stays alive between builds waiting for the next build. This allows data and code that is likely to be required in the next build to be kept in memory, ready to go. This dramatically improves the performance of subsequent builds. Enabling the Gradle Daemon is an extremely cheap way to decrease build times.

If you're not convinced yet, head over to the setup instructions. It won't take you more than five minutes, and you'll have that time back at the end of your work day!

Update Your Gradle Version

Another super fast option is to update the used Gradle version to 2.4 (or newer). It reduced the build times quite a bit for us! Check your build version in Android Studio at:

  • File > Project Structure > Project > Gradle Version.

Automated Resource Shrinking with Gradle

Most Android developers are aware of ProGuard, the code obfuscating tool. It also strips away unused code and methods to make your apk smaller. Actually quite a while ago Google added a new option to further reduce the size of apps by removing even more unused resources: resource shrinking.

All you've to do is to enable it in your build.gradle:

android {  
    ...

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
        }
    }

   ...
}

Read here for more details.

Automatically Add the Git Revision to Your Project

It's often very helpful to being able to track down a bug or crash report to a specific version. Version codes help, but are not very accurate. On the other hand, the git revision can specify the problematic version very accurately. Adding the git revision by hand would be painful, so let's look at this small build.gradle script enhancement:

android {  
    buildTypes {
        debug {
                resValue "string", "git_revision", "\"${gitRevision()}\""
        }

        release {
                resValue "string", "git_revision", "\"${gitRevision()}\""
        }
    }
}

def gitRevision() {  
    def cmd = "git rev-parse --short HEAD"
    return cmd.execute().text.trim()
}

It adds the 'git_revision' string resource to your project. It automatically updates it with each build and you've it easily accessibly in your app via a getResources().getString( R.string.git_revision );. You could use this line to add it to your bug reporting tool and it'll make your bug fixing process much easier.

Thanks for this tip to coderwall.com and stackoverflow.

If you enjoyed this quick tip post, let us know what topic you're interested in hearing more small tips to make your day a little better.

Explore the Library

Find interesting tutorials and solutions for your problems.