extraPropertiesExtension

Here’s a quick tip that’ll save you a few seconds every now and then: If your apps are anything like mine, you will have a dependency list that looks a little like this:

dependencies {
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    ...
    compile 'com.google.android.gms:play-services-ads:8.3.0'
    compile 'com.google.android.gms:play-services-appindexing:8.3.0'
    compile 'com.google.android.gms:play-services-base:8.3.0'
    ...
}

Whenever there’s a new version of Google Play Services or the Support Library you probably go through and update each line one at a time. Smarter developers will Find/Replace to save a few seconds. And even smarter developers will do this:

ext {
    playServices = '8.3.0'
    supportVersion = '23.1.1'
}

dependencies {
    compile "com.android.support:appcompat-v7:${supportVersion}"
    compile "com.android.support:recyclerview-v7:${supportVersion}"
    compile "com.android.support:cardview-v7:${supportVersion}"
    compile "com.android.support:design:${supportVersion}"
    ...
    compile "com.google.android.gms:play-services-ads:${playServices}"
    compile "com.google.android.gms:play-services-appindexing:${playServices}"
    compile "com.google.android.gms:play-services-base:${playServices}"
    ...
}

Head smack!

So what’s happening here?

Gradle provides this neat thing called ExtraPropertiesExtension. The whole point of it is to store arbitrary key/value pairs that you can use elsewhere in your build files, or, as Gradle puts it:

All enhanced objects in Gradle’s domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object’s ext property. Alternatively, an ext block can be used to add multiple properties at once.

Source: https://docs.gradle.org/current/userguide/writing_build_scripts.html

Epilogue

I encourage you to take a deep dive into the Gradle documentation. As with most things Gradle related, we’ve really only scratched the surface of its power. And complexity. I think many Android developers are guilty of looking at Gradle as the thing that builds our apps. The thing we keep on syncing. The thing we keep on breaking. But it’s so, so much more than that. As they say: “Gradle makes the impossible possible, the possible easy and the easy elegant.”

About The Author

A little biographical info.

Close