r/Kotlin • u/Pac-ynka • 7h ago
Gradle is very confusing
Imagine a project that consists of three apps (mobile, server, desktop).
Most people would see it this way:
sh
horse-tinder # Project
│
├── build.gradle
├── settings.gradle
├── gradle.properties
│
├── server # Module
│ └── build.gradle
│
├── mobile # Module
│ └── build.gradle
│
└── desktop # Module
└── build.gradle
But in Gradle whole product is called a build not a project. Where sub-parts of a build are called projects not modules. So more accurate description looks like this:
sh
horse-tinder # Project (as a gradle project) | Build (Gradle powered project)
│
├── build.gradle # Optional configuration file for root project.
├── settings.gradle # Defines a build. Isn't strictly required (a single-project build runs without it)
├── gradle.properties # Variables that configure Gradle binary itself.
│
├── server # Project
│ └── build.gradle
│
├── mobile # Project
│ └── build.gradle
│
└── desktop # Project
└── build.gradle
What is a Build?
- Is defined by
settings.gradlefile (notbuild.gradle). - Includes set of projects or a single project.
- A build can refere to multiple things:
- The build meaning whole gradle project.
- Task called build.
- Product/output of a build task.
- Process of building a project.
- Process of building the build.
What is a Project?
- They get defined using tree structure in
settings.gradlefile:
groovy
include(":libs:a")
include(":libs:b")
include(":server")
include(":client")
- Each node (libs, a, b, server, client) in this tree defines a separate project.
- By default Gradle looks for projects using same directory tree. But path to each project can be manually defined.
sh
horse-tinder
│
├── server
│ └── build.gradle
│
├── client
│ └── build.gradle
│
└── libs
└── a
└── build.gradle
└── b
└── build.gradle
- Intermediate nodes in a tree (eg. libs) do not require their own
build.gradle. - Root project exists by default. Root
build.gradleis optional configuration file. Its not something to be included insettings.gradle. - Project can be addressed either by a absolute path (eg.
:composeApp) or a relative path (eg.composeApp).
What is a Task?
- A unit of work.
- Always belongs to specific project.
What are Gradle properties?
- Variables in format of key-value pairs defined in
gradle.properties. - Private defaults are stored in
~/.gradle/gradle.properties
What is versions file?
- Central list of versions and libraries defined in
libs.versions.tomlfile. - Stored in
gradlefolder.
What is a Gradle wrapper?
- A small launcher that bundled with a repository that downloads the pinned Gradle version (from
gradle-wrapper.properties) on first use and reuses the cached copy after.
Why it is so confusing?
People: These are modules of a project. These modules can consist of sub-modules.
Gradle: No! These are projects of a single build. Projects are organized into a tree structure but it means nothing other than organization.
People: This build.gradle file probably has something to do with a build.
Gradle: Wrong guess again! These files describe projects.
People: If projects are organized into a tree structure then root build.gradle must be very important.
Gradle: Naaah. Its optional.
People: So whole project is called a build and modules are in fact called projects which are defined by files called build.gradle?!
Gradle: Exactly!
So to avoid confusion build.gradle should be named module.gradle and settings.gradle should be named build.gradle.
