Understanding Gradle

Adopted by all Android developers and now the industry standard. But unfortunately not everyone understands how it works. I’ve been as well pretty ignorant about it until very recently.

Apply Plugin

This is the core of any build.gradle file.

Gradle has plenty of plugins that define conventions for project structures, tasks and build processes. 99% of Android developers will be happy with ‘com.android.application’ while others would like to redefine some of its parameters.

Java developers can start a simple java project with a minimalist build.gradle that only contains the Apply Plugin command, as long as they follow the project structure conventions.

Wrapper

The mysterious graglew script that accompanied our build.gradle is a distributable version of the Gradle application. You can specify which version of gradle to use, so your coworkers won’t have to deal with tool versioning.

Tasks

You can easily define new tasks and dependencies between them.

The best part is that you can extend existing tasks, for example, you can write a copy task that replaces some placeholder text by extending the existing copy task.

This kind of custom tasks are very useful to copy binary resources from outside your git into your project.

And of you can make some tasks dependent of others with the following examples:

Dependencies

Another powerful feature of Gradle is dependency management.

You can define dependencies across tasks, projects or private and public libraries.

The following example shows how two subprojects can be related. (Project2 depends of Project1)

Both need to be defined in a settings.gradle, and so the dependency can be defined in the main build.gradle.


This is just the top of the iceberg. Android and Java developers have no excuse to not to use Gradle on their projects. It has streamlined the Android build process and it’s way easier to use than Ant or Maven on Java projects.

You can find some examples on how to use Gradle in this GitHub repo.

And if you are in Pluralsight, I recommend you Gradle Fundamentals.


Thanks for getting this far! I hope you enjoyed this publication. If you think it was worth reading, please share it and more people will benefit from it. You can also click on the Recommend button if you are Medium user. And of course, feel free to leave your feedback!

You can follow my updates on Twitter and LinkedIn. And find more code examples in my GitHub page.