Skip to main content

Gradle Tutorial

Gradle is a powerful build automation tool used for Java, Android, and many other languages. It provides flexibility and control over build processes and dependency management, making it popular for large-scale projects.


1. Installing Gradle​

Step 1: Download Gradle​

  1. Go to the Gradle website and download the latest release.
  2. Unzip the downloaded file and add the bin directory to your system's PATH.

Step 2: Verify Installation​

Check if Gradle is installed by running:

gradle -v

2. Creating a Gradle Project​

  1. Open a terminal and navigate to your desired directory.

  2. Run the following command to create a new Gradle project:

    gradle init
  3. Choose the project type (e.g., application, library) and language (e.g., Java). Gradle will create a basic project structure with files like build.gradle.


3. Understanding build.gradle​

The build.gradle file is the core of a Gradle project. It defines dependencies, plugins, and custom tasks.

Example build.gradle for a Java Project​

plugins {
id 'java'
}

group = 'com.example'
version = '1.0'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'junit:junit:4.13.2'
}

Key Sections​

  • plugins: Defines plugins to extend Gradle’s functionality.
  • repositories: Specifies where to find dependencies (e.g., Maven Central).
  • dependencies: Defines libraries and frameworks needed for the project.

4. Common Gradle Commands​

  • Build: Compile source code and generate output files.

    gradle build
  • Clean: Delete build files and start fresh.

    gradle clean
  • Run Tests: Execute tests defined in the project.

    gradle test
  • Execute Application: Run the main application class (if it’s an application project).

    gradle run
  • Generate JAR: Package the project into a JAR file.

    gradle jar

5. Managing Dependencies​

Dependencies are managed in the dependencies block in build.gradle.

Example​

dependencies {
implementation 'org.springframework:spring-core:5.3.8'
testImplementation 'junit:junit:4.13.2'
}
  • implementation: Adds a dependency for runtime and compile-time use.
  • testImplementation: Adds a dependency only for testing.

6. Using Gradle Wrapper​

The Gradle Wrapper allows you to run a specific Gradle version, ensuring consistency across environments.

Adding the Wrapper​

To add the Gradle Wrapper to your project, run:

gradle wrapper

This creates files like gradlew (a shell script) and gradlew.bat (for Windows).

Running with the Wrapper​

Instead of using gradle commands, use ./gradlew:

./gradlew build

7. Custom Tasks​

Gradle allows you to define custom tasks in build.gradle.

Example​

task hello {
doLast {
println 'Hello, Gradle!'
}
}

Run the custom task with:

gradle hello

8. Multi-Project Builds​

Gradle supports multi-project builds for managing large applications with multiple modules.

Example Structure​

rootProject/
β”œβ”€β”€ settings.gradle
β”œβ”€β”€ build.gradle
β”œβ”€β”€ app/
β”‚ └── build.gradle
└── lib/
└── build.gradle

Defining Modules​

In settings.gradle:

rootProject.name = 'MyProject'
include 'app', 'lib'

Each sub-project can have its own build.gradle, with dependencies shared or isolated as needed.


Summary​

This tutorial covered the basics of Gradle:

  1. Setting up Gradle and creating a new project.
  2. Understanding build.gradle for dependency management and configuration.
  3. Running common Gradle commands for building, testing, and cleaning.
  4. Using Gradle Wrapper for consistent builds.
  5. Creating custom tasks and managing multi-project builds.

Gradle is versatile and customizable, making it a strong choice for automating project builds and dependency management.

Content Review​

The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.

About the Reviewer​

For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.