Every later lesson assumes you know what compiling, dependencies, tests and tasks are, because those are the jobs Gradle organises.
A program is a set of instructions for a computer. Programmers write them as text in a programming language, a precise written language computers can follow, and that text is the source code. Gradle is mostly used with Java and Kotlin, two popular languages for the computers behind websites, for desktop tools and for Android phone apps.
The computer can't run that text directly. A compiler first translates it into a lower-level form, and that step is called compiling. Mistakes the compiler finds stop the build with an error message.
Java and Kotlin compile to bytecode: instructions for the JVM (Java Virtual Machine), a program that runs bytecode on Windows, macOS and Linux alike. Programmers install the JVM together with the compiler and other tools as a JDK (Java Development Kit).
Almost no program is written from scratch. It uses libraries, ready-made code written by others, for jobs like reading files or sending email. A library your project needs is a dependency. Libraries are downloaded from a repository, an online store of published libraries; the biggest for Java is Maven Central.
Programmers also write automated tests: small extra pieces of code that run the program's code with known inputs and check the answers, so that a bug, a mistake that makes a program misbehave, is caught early.
Finally, the compiled code is packed into one file to run or hand out, usually a JAR (Java ARchive), a zip-like file of compiled code. Any file a build produces for others to use is called an artifact.
Doing all this by hand, in the right order, every time, is slow and error-prone. A build tool automates it: you describe your project once, and it downloads the dependencies, compiles, tests and packs everything up. Gradle is a build tool. It splits the work into named steps called tasks, such as compileJava, test and jar, and runs them in the right order.
You drive Gradle by typing commands in a terminal, the text window where you give the computer instructions by typing. ./gradlew build means "run the build task, and every task it needs first". Gradle remembers what it did last time and skips tasks whose inputs haven't changed, which is why a second build is much faster.
Example
my-app/
settings.gradle.kts # names the project
build.gradle.kts # describes how to build it
gradlew # starts Gradle (next lesson)
src/main/java/App.java # your source code
src/test/java/AppTest.java # your tests
$ ./gradlew build
> Task :compileJava # source code -> bytecode
> Task :test # run the tests
> Task :jar # pack into build/libs/my-app.jar
BUILD SUCCESSFULRemember
- Source code is compiled to bytecode, which the JVM runs
- Dependencies are libraries your project uses, downloaded from repositories
- A build tool compiles, tests and packs up your code for you, as a series of tasks
Watch out for
- Thinking Gradle itself is broken when a build fails; the error message usually points to a mistake the compiler or a test found in your code.