What is Gradle?
Gradle is an open-source build automation tool used mainly for:
Building Java/Kotlin projects
Android app development
Managing dependencies
Running tests
Packaging applications
It replaces older tools like Ant and Maven.
In simple words:
Gradle automates the process of compiling, testing, and packaging your project.
It uses:
Groovy DSL or
Kotlin DSL (.[Link] files)
Advantages of Gradle
Fast builds – Uses incremental builds (only builds changed files).
Flexible & customizable – Highly configurable.
Powerful dependency management – Automatically downloads required libraries.
Supports multiple languages – Java, Kotlin, C++, etc.
Android official build tool – Used by Android Studio.
Disadvantages of Gradle
Complex for beginners – Harder than Maven at first.
Build scripts can become complicated in large projects.
More memory usage compared to simpler tools.
Simple Gradle Workflow Diagram
Developer runs: gradle build
↓
Initialization
↓
Configuration (Create Task Graph)
↓
Execution (Run Required Tasks)
↓
Output: .jar / .apk / .war
What is Groovy?
o Groovy is a high-level, object-oriented programming language that runs on the Java Virtual
Machine (JVM).
o It is designed to make Java programming easier and shorter.
o Groovy uses Java syntax but with less code (less boilerplate).
👉 In simple words: Groovy = Simplified Java language that runs on the JVM.
Key Features of Groovy
1. Runs on JVM
Groovy programs run on the Java Virtual Machine, so they can use Java libraries and frameworks.
Example: You can use Java classes directly in Groovy.
2. Less Code (Concise Syntax)
Java program: [Link]("Hello World");
Groovy program: println "Hello World"
Groovy removes unnecessary syntax.
3. Dynamic Typing
In Java you must declare the type:
String name = "Ravi";
In Groovy:
def name = "Ravi"
def automatically detects the data type.
4. Supports Both Dynamic and Static Programming
Groovy can work as:
Dynamic language (like Python)
Static language (like Java)
Example:
def x = 10
def y = 20
println x + y
5. Used in Build Tools
Groovy is widely used in build automation tools like: Gradle
Example Gradle build script:
plugins {
id 'java'
}
6. Easy Integration with Java
Groovy code can use Java libraries:
import [Link]
def today = new Date()
println today
Structure of a Simple Groovy Program
class Hello {
static void main(String[] args) {
println "Hello Groovy"
}
}
Output:
Hello Groovy
Advantages of Groovy
✔ Simple syntax
✔ Less code than Java
✔ Runs on JVM
✔ Supports scripting and application development
✔ Integrates with Java easily
Where Groovy is Used
Gradle build scripts
Automation testing (Jenkins pipelines)
Scripting: Web frameworks (like Grails)
Example CI/CD tool using Groovy: Jenkins
🔹 What is Kotlin?
Kotlin is a modern programming language developed by JetBrains.
It runs on:
JVM (Java Virtual Machine)
Android
Web (Kotlin JS)
Native platforms
👉 It is officially supported by Google for Android development.
Example (Simple Kotlin Code)
fun main() {
println("Hello, World!")
}
Advantages of Kotlin
Less code than Java – More concise.
Null safety – Reduces NullPointerException errors.
Fully compatible with Java – Can use Java libraries easily.
Modern features – Lambdas, coroutines, extension functions.
Official Android language – Preferred over Java.
Disadvantages of Kotlin
Compilation speed sometimes slower than Java.
Smaller community than Java (but growing fast).
Learning curve for beginners who know only basic Java.
🔹 Simple Comparison
Feature Gradle Kotlin
Type Build Tool Programming Language
Used For Building & managing projects Writing application code
Used In Java, Android, backend projects Android apps, backend, web
Replacement Of Maven / Ant Java (in many cases)
Kotlin JVM Workflow
Kotlin Source Code (.kt)
↓
Kotlin Compiler
↓
Bytecode (.class)
↓
JVM
↓
Program Output
Android Kotlin Workflow (Combined with Gradle): When building an Android app:
You write Kotlin code
Gradle manages dependencies
Kotlin compiler converts code
Gradle packages into APK
App runs on Android device
🔥 How Gradle + Kotlin Work Together:
You write Kotlin code
↓
Gradle starts build
↓
Gradle calls Kotlin compiler
↓
Bytecode generated
↓
Gradle packages app
↓
App runs
Short Summary
| Gradle | Kotlin |
| --------------------- | ------------------------ |
| Manages build process | Writes application logic |
| Controls tasks | Compiled to bytecode |
| Packages project | Runs on JVM/Android |
Command : gradle init --type java-application
This command tells Gradle to:
✅ Create a new Java application project automatically.
It generates a complete project structure for you.
You don’t need to manually create folders or build files.
What Happens Internally:
Step-by-step:
Gradle creates project folder structure
Generates [Link]
Generates [Link]
Creates src/main/java
Creates src/test/java
Adds sample [Link]
Adds sample [Link]
Creates Gradle Wrapper files
Project Structure Created
project-name/
├── [Link]
├── [Link]
├── gradlew
├── [Link]
├── gradle/
│ └── wrapper/
│ ├── [Link]
│ └── [Link]
└── src/
├── main/java/
│ └── [Link]
└── test/java/
└── [Link]
Important Files Explained
1️[Link]
Contains:
Plugins
Dependencies
Application configuration
2️[Link]
Contains project name: [Link] = 'demo'
3️ gradlew (Gradle Wrapper): This allows you to run: ./gradlew build
Even if Gradle is NOT installed globally.
Very important in companies.
🔹 After Running This Command
You can run:
▶ Build project
gradle build
▶ Run project
gradle run
▶ Run tests
gradle test
🔹 What Is --type java-application?
Gradle supports different project types:
Type Purpose
java-application Java CLI app
java-library Java reusable library
kotlin-applicationKotlin app
kotlin-library Kotlin library
groovy-application Groovy app
So this command creates: A runnable Java application project
Real Development Workflow
gradle init --type java-application
↓
Modify [Link]
↓
gradle build
↓
gradle run
↓
gradle test
What This [Link] File Does
This configuration is for a Java Application Project using Gradle.
1️Plugins Section
plugins {
id 'application'
}
✅ What it does: Applies the Application Plugin
Allows you to: Run the program using gradle run
Create a distributable package
Define the main class
👉 Without this plugin, Gradle cannot run your Java program directly.
2️Application Block
application {
mainClass = '[Link]'
}
✅ What it does: Specifies the main class containing:
public static void main(String[] args)
Gradle will start execution from:
[Link]
📌 Make sure: The package name matches your folder structure
The class contains a main() method
3️Repositories Section
repositories {
mavenCentral()
}
✅ What it does:
Tells Gradle where to download dependencies
mavenCentral() is a public repository
Gradle downloads:
JUnit
Other libraries if added
4️Dependencies Section
dependencies {
testImplementation 'junit:junit:4.13.2'
}
✅ What it does:
Adds JUnit 4.13.2 for testing
testImplementation means:
Used only during testing
Not included in final application
It downloads JUnit from Maven Central.
5️Test Configuration
test {
[Link] { false }
testLogging {
events "passed", "failed", "skipped"
exceptionFormat "full"
showStandardStreams = true
}
}
✅ What this block does:
🔹 [Link] { false }
Forces tests to run every time
Even if nothing changed
🔹 testLogging
Controls test output display:
Option Meaning
events Shows passed/failed/skipped tests
exceptionFormat "full" Shows full stack trace
showStandardStreams = true Shows [Link]() output
🔄 Workflow When You Run Commands
gradle build:
Initialization
Configuration
Compile Java
Run tests
Create .jar
gradle run:
Compile Java
Run main class:
[Link]
📂 Expected Project Structure
project/
├── [Link]
├── [Link]
└── src/
├── main/java/com/example/[Link]
└── test/java/com/example/[Link]
🔥 What This Project Type Is
This is a:
✅ Java CLI Application
✅ Uses Gradle
✅ Uses JUnit for testing
Groovy- Project - [Link]
package [Link];
public class AdditionOperation {
public static double add(double num1, double num2) {
return num1 + num2;
}
public static void main(String[] args) {
double result = add(5, 10);
[Link]("Sum: " + result);
}
}
Explanation: public static void main(String[] args)
We created a separate method:
public static double add(double num1, double num2)
Now:
Method Purpose
add() Contains business logic
main() Only runs the program
This is called:
✅ Separation of Concerns
🔁 Program Flow Now
main() → calls add(5,10) → returns 15 → prints result
🔹 Why This Is Important?
Because in real projects:
Business logic must be independent
Logic must be reusable
Logic must be testable
Without this separation:
Testing becomes difficult
Code becomes messy
Step 4: [Link] (JUnit Test)
package [Link];
import [Link];
import static [Link].*;
public class AdditionOperationTest {
@Test
public void testAddition() {
double expected = 15.0;
double actual = [Link](5, 10);
assertEquals(expected, actual, 0.01);
}
}
Explanation :
1️@Test
Marks this method as a test method for JUnit.
You are using: JUnit (version 4.13.2)
2️Calling Real Method: double actual = [Link](5, 10);
Now the test:
Calls real add() method
Gets real output
Compares result
3️Assertion : assertEquals(expected, actual, 0.01);
Meaning: Expected = 15.0
Actual = Result from method
0.01 = tolerance for decimal comparison
If:
|expected - actual| <= 0.01
→ Test passes
Otherwise:
→ Test fails
🔄 Complete Execution Flow With Gradle
When you run: gradle test
Gradle does: Compile [Link]
Compile [Link]
Run Junit: Compare expected vs actual
Generate test report
Step 2: [Link] (Kotlin DSL)
plugins {
kotlin("jvm") version "1.8.21"
application
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation("junit:junit:4.13.2")
}
application {
[Link]("[Link]")
}
[Link] {
useJUnit()
testLogging {
events("passed", "failed", "skipped")
exceptionFormat = [Link]
showStandardStreams = true
}
[Link] { false }
}
java {
toolchain {
[Link]([Link](17))
}
}
Step 3: [Link] (Change file name and update below code) Build Script Assistance
Manually navigate the folder path like src/main/java/org/example/
Change the file name [Link] to [Link]
After then open that file and copy the below code and past it, save it.
package [Link]
fun addNumbers(num1: Double, num2: Double): Double {
return num1 + num2
}
fun main() {
val num1 = 10.0
val num2 = 5.0
val result = addNumbers(num1, num2)
println("The sum of $num1 and $num2 is: $result")
}
//Package Declaration
package [Link]
This means your file must be inside: src/main/kotlin/com/example/
If file name is: [Link]
Then Gradle runs: [Link]("[Link]")
Because Kotlin converts [Link] → [Link].
2️Function: addNumbers
fun addNumbers(num1: Double, num2: Double): Double {
return num1 + num2
}
🔹 What this means:
Part Meaning
fun Keyword to define function
addNumbers Function name
num1: Double Parameter with type
: Double Return type
This function: Takes two decimal numbers
Returns their sum
3️main() Function
fun main() { //This is the entry point of the program.
In Kotlin: No need for public static void
Simpler than Java
4️Variables:
val num1 = 10.0
val num2 = 5.0
🔹 val means:
Immutable (cannot change value)
Similar to final in Java
If you wanted mutable: var num1 = 10.0
5️Calling Function: val result = addNumbers(num1, num2)
Program flow:
main()
↓
addNumbers(10.0, 5.0)
↓
returns 15.0
6️String Interpolation
println("The sum of $num1 and $num2 is: $result")
🔹 $variable = String interpolation
Instead of Java: [Link]("Sum: " + result);
Kotlin is cleaner and more readable.
🔄 Execution Flow with Gradle
When you run: ./gradlew run
Gradle will:
1. Read [Link]
2. Apply Kotlin JVM plugin
3. Compile .kt files
4. Generate [Link]
5. Run main()
6. Print output
🔥 Output
The sum of 10.0 and 5.0 is: 15.0
gradle build: This is the complete build lifecycle command.
It performs:
1. Compile source code
2. Process resources
3. Compile test code
4. Run tests
5. Package application (JAR file)
6. Generate reports
Internally Runs These Tasks
Compile Java / compile Kotlin
Process Resources
classes
compile Test Java / compile Test Kotlin
test
jar
build
Output Generated
After running: gradle build
You will see:
build/
├── classes/
├── libs/
│ └── [Link]
├── reports/
└── test-results/
Main file created:
build/libs/[Link]
🔹 2️gradle run: gradle run
What It Does: Runs your application using the Application Plugin.
It:
1. Compiles code (if needed)
2. Finds main class
3. Executes main() method
4. Shows output in terminal
🔄 Flow
gradle run
↓
Compile source
↓
Run mainClass
↓
Print output
Example output: The sum of 10.0 and 5.0 is: 15.0
⚠ Requires:
plugins {
id 'application'
}
or in Kotlin DSL:
application
And:
[Link]("[Link]")
3️gradle test: gradle test
✅ What It Does : Runs only the unit tests.
It performs:
1. Compile main code
2. Compile test code
3. Run JUnit tests
4. Show results
5. Generate test report
📊 Test Report Location: build/reports/tests/test/[Link]
Open in browser to see:
Passed tests
Failed tests
Stack trace
You are using: JUnit
🔥 Difference Between Commands
Command Purpose Runs Tests? Creates JAR?
gradle build Full build ✅ Yes ✅ Yes
gradle run Run app ❌ No ❌ No
gradle test Run tests only ✅ Yes ❌ No