Gradle
Overview
- Gradle is an open-source build automation tool
- Build scripts are written in Groovy or Kotlin
- Official build tool for Android
- Extensible with plugins
- Every existing Gradle projects ships with a Gradle wrapper, which means you don’t have to install Gradle to build the project
gradlew
for Linuxgradlew.bat
for Windows
- You can either use Gradle as a CLI or through IDE plugins
Core Model
- Builds are modeled as _Directed Acyclic Graphs (DAGs) of tasks (units of work)
- Tasks are wired together based on their dependencies
Tasks consist of
- Actions: pieces of work that do something, like copy files or compile sources
- Inputs: values, files and directories that actions operate on
- Outputs: files and directories that actions modify or generate
There are several build phases:
- Initialization: Setup the environment
- Configuration: Constructs and configures the task graph
- Execution: Run selected task
Build scripts should be declarative instead of imperative
Build scripts map to the Gradle API
Build Environment
- There are multiple ways of configuring Gradle:
- Command-line flags: Have precedence over properties and environment variables
- Example:
--build-cache
- Example:
- System properties
- Example:
systemProp.http.proxyHost=somehost.org
stored ingradle.properties
- Example:
- Gradle properties
- Example:
org.gradle.caching=true
stored ingradle.properties
- Example:
- Environment variables
- Command-line flags: Have precedence over properties and environment variables
Gradle Daemon
- Is a long-lived process used for caching
- It waits in between builds in idle mode
- Allows Gradle to be loaded into memory only once, instead of for each build
Simple build.gradle
import org.apache.commons.codec.binary.Base64
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: "commons-codec", name: "commons-codec", version: "1.2"
}
}
task hello {
doLast {
println "Hello World!"
}
}
task upper {
dependsOn hello
doLast {
String str = "waynee95"
println "Original: $str"
println "Upper case: ${str.toUpperCase()}"
}
}
Maven vs Gradle
Kotlin DSL
Cheatsheet
Run a task
$ gradle <taskname>
Use gradle -q <taskname>
to suppress log messages.
Most builds have clean
, check
, assemble
and build
tasks.
List all available tasks
$ gradle tasks
To learn more about any task run gradle help --task <taskname>
.
Update gradle wrapper version
$ gradle wrapper --gradle-version=6.4
Run custom jar file
Add this to build.gradle
:
task runJflex(type: JavaExec) {
classpath = files("libs/jflex-full-1.8.2.jar")
args '/path/to/flex/file'
}