Gradle Build Tool I Modern Open Source Build Automation
Recall what are the steps involved in the running of a Java program, the program is first compiled and bytecode is created at the compile time. At the runtime, classes are loaded, bytecode is verified and then the interpreter reads the bytecode and executes the instructions.
How does this happen?
We simply click on Build and then Run, or just Run (Build automatically happens). Have you ever wondered that why there wasn’t any explicit need of giving the commands for loading the classes, creating bytecodes or linking etc? This is where the automation system comes.
To quote, “Build Automation is the process of automating the creation of a software build and the associated processes including compiling source code into bytecodes, packaging binary code and running automated tests.”
As an example, we usually click on “Build” option, the system itself carries out all the processes needed for the running of our source code i.e.It automatically executes all the tasks (compiling, loading, linking, packaging etc) and runs our code. Various other automation systems exist like Apache Ant and Apache Maven. Every such system follows some conventions. The quote, “convention rather than configuration” is very famous in this world of automated builds.
Assume you have created a project with lots of classes and resources (eg. image and layout files). All the files are kept in one folder and you give this project to someone else. Will it not be tough for that person to understand? Thus, the build system follows certain conventions. Like every project should have the structure like this:
- src folder: contains all classes
- res folder: contains all the resources
- manifest folder: containing project’s configurations.
With this, the other person will know that whole of the source code is kept under src folder and all the resources under res folder. Similar way, other conventions for keeping test files and build files are there as per the build system used by the application.
With this, we understand the meaning of the statement that “Gradle is a build Automation system”.
- Domain Specific Language: We use domain-specific languages to describe tasks related to automating the analysis and transformation of source code.It is a computer language specialized to a particular application domain. These are small, expressive language custom designed for specific tasks. An example is HTML for web pages.
The word domain refers to an “area or sphere of knowledge, influence or activity” i.e. DSLs are keenly focussed on a certain type of problems or domains. Focussing on a domain gives us a context which defines a logical framework within which we can evolve models for an application i.e. given some domain, we can logically imply what all properties and rules need to be defined for solving a problem specific to that area of interest.
- The word specific gives us the bounded context and helps keep things relevant and focussed.
Ex: if you have encountered using “makefile” on Linux systems, the makefile is a DSL used to specify rules and dependencies for building an application.
Simplicity is critical to the success of DSL. A person familiar with the language domain must easily understand it. We can define the dependencies for the execution of a particular task in DSLs. Ex, if we are creating DSLs to express business rules in the domain of insurance, it should be built on their vocabulary i.e. terms like the loan, risk, validity should be used, the terms people use every day to communicate with others so that it is easy to understand, evolve and maintain. It should seem that the DSL is merely specifying some discrete rules rather than giving a feel of programming.
It is an object oriented language which can also be used as a scripting language for the Java platform. It supports both dynamic and static typing. It is very similar to java in the syntactical sense but has various other stuff. By default, it includes various libraries also.
Install Groovy on your system and on the console, try running some java codes.
In the above screenshot, we didn’t include any libraries, simply printed Hello world the way we do in Java. As per the specifications, semicolons, brackets, etc can be eliminated in Groovy. Refer to the official documentation for that. Groovy is a language that underlies Gradle and all the scripts in grade are DSLs implemented using Groovy.
- Minimize Configuration Required for New Projects : Gradle has a set of default configuration settings that are automatically applied to every project you create in Android Studio. If you’re developing a project that doesn’t adhere to these default configuration rules, Gradle is easy to customize.
- Declare Project Dependencies: Dependencies can be modules, JAR files or libraries, and they can be located either on the local file system or a remote server.If you have used Eclipse, you must have encountered a situation in which you are required to add some external JARs in your applications. You explicitly have to go, download them properly as per the required version and add them in the classpath variable. Gradle is efficient in the sense that it automatically resolves all the build dependencies by just specifying the needed ones in the configuration files. It itself downloads and caches as per the compatible version.
- Test Your Project: Gradle automatically generates a test directory and a test APK from your project’s test sources and can run your tests during the build process.
- Beats all other automation systems: Gradle is built on top of existing build systems by combining the best features of systems like Apache Ant and Maven and eliminates their disadvantages. The most featured of them is it doesn’t use XML. Developers call it eliminating the noise. Consider the below screenshot which displays one configuration file in Apache Maven and Gradle.
We see the difference in code length and readability too. Because of all these features with several others like support for multi-projects etc gives Gradle the rank 1.
This article is contributed by Ekta Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.