agnostic Build System
100% Free Open Source - Apache Standard License 2.0
JVM based
Multi-ecosystem
JVM: Java, Kotlin, Groovy, Scala, …
Native: C/C++, Go, …
Android
…
Volker Leck (@devisnik) - Gradle Inc.
Gradle is a build and automation tool.
agnostic Build System
100% Free Open Source - Apache Standard License 2.0
JVM based
Multi-ecosystem
JVM: Java, Kotlin, Groovy, Scala, …
Native: C/C++, Go, …
Android
…
The company behind Gradle.
Builds Happiness
Employs full time engineers
Provides Gradle Build Scans and Gradle Enterprise
Consulting and Training
30 seconds waste * 50 builds * 10 developers
> 4 hours wasted time per day
monitors build
ensures build health
fixes regressions
In a healthy culture, developers and engineering leaders understand that the domain of building and integrating software is just as complex and challenging as the domain of application development.
define scenario to improve
profile it
identify biggest bottleneck
fix it
verify (by measurement)
check-no-tests {
tasks = ["check"]
gradle-args = ["-x", "test"]
cleanup-tasks = ["clean"]
warm-ups = 2
}
androidStudioSync {
android-studio-sync { }
}
./gradlew wrapper --gradle-version 5.5
classpath 'com.android.tools.build:gradle:3.4.1'
org.gradle.jvmargs=-Xmx4G
org.gradle.daemon = true
org.gradle.caching = true
org.gradle.parallel = true
google() {
content {
includeGroupByRegex "com\\.android\\..*"
includeGroupByRegex "com\\.google\\.android\\..*"
includeGroupByRegex "androidx\\..*"
}
}

./gradlew --scan http://scans.gradle.com



initialization > 1s
long configuration
single line change ~= clean build
high gc time
Don’t do this!
// logic in settings.gradle
// brittle, can be slow!
new File('.').eachFileRecurse(groovy.io.FileType.DIRECTORIES) { dir ->
if (new File(dir,"build.gradle").exists()) {
def name = dir.toString()[1..-1].replaceAll('/',':')
include name
}}
Evaluating build scripts
Applying plugins
Running afterEvaluate {} blocks
on any Gradle run
Even ./gradlew help or ./gradlew tasks
Android Studio sync

Be aware of inefficient plugins!

Avoid Dependency resolution at configuration time!
task fatJar(type: Jar) {
from configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
with jar
classifier = 'fat-jar'
}
task fatJar(type: Jar) {
from {
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
classifier = 'fat-jar'
}
tasks.register("fatJar", Jar) {
from configurations.runtimeClasspath.elements.map {
it.isDirectory() ? it : zipTree(it)
}
with jar
classifier = 'fat-jar'
}

That build script seems expensive
task projectStats {
def statsFile = new File(buildDir, 'stats.txt')
statsFile.parentFile.mkdirs()
def javaFiles = sourceSets.main.java.size()
def javaSize = sourceSets.main.java
.collect{it.text.bytes}
.flatten()
.sum()
statsFile.text = """
|SourceFiles: ${javaFiles}
|Source size: ${javaSize} bytes
""".stripMargin()
}
Careful with custom tasks!
task projectStats {
def statsFile = new File(buildDir, 'stats.txt')
input.files sourceSet.main.java
outputs.file statsFile
doLast {
statsFile.parentFile.mkdirs()
def javaFiles = sourceSets.main.java.size()
def javaSize = sourceSets.main.java
.collect{it.text.bytes}
.flatten()
.sum()
statsFile.text = """
|SourceFiles: ${javaFiles}
|Source size: ${javaSize} bytes
""".stripMargin()
}
}
Don’t forget doLast{}
task projectStats(type: ProjectStats) {
statsFile = new File(buildDir, 'stats.txt')
sources = sourceSet.main.java
}
class ProjectStats extends DefaultTask {
@InputFiles FileCollection sources
@OutputFile File statsFile
@TaskAction def stats() {
statsFile.text = """
|Files: ${sources.size()}
|Total: ${sources.collect{it.text.bytes}.flatten().sum()} bytes
""".stripMargin()
}
}
// version.gradle
def out = new ByteArrayOutputStream()
exec {
commandLine 'git','rev-parse','HEAD'
standardOutput = out
workingDir = rootDir
}
version = new String(out.toByteArray())
// root 'build.gradle'
allprojects {
apply from:'$rootDir/version.gradle'
}
// root 'build.gradle'
apply from:"$rootDir/version.gradle"
subprojects {
version = rootProject.version
}
variantFilter { variant ->
def flavorName = variant.flavors[0].name
def devFlavor = flavorName == 'dev'
if(!devFlavor && variant.buildType.name == 'release') {
variant.ignore = true
}
}
Executing selected tasks
Incremental
Cacheable
Parallelizable
Nothing changed and outputs still there?
Executed tasks should be zero!


Modularization ⇒ Compile avoidance
Decoupled code ⇒ Faster incremental compilation
careful with annotation processing
since Gradle 4.7
processors need to opt-in
kapt.incremental.apt = true since kotlin 1.3.30
javaCompileOptions {
annotationProcessorOptions {
arguments << ["dagger.gradle.incremental": "true"]
}
}
Task has been run with same inputs before?
Pull its outputs from cache
org.gradle.caching = true

Many tasks are cacheable

input hash changes across environments
due to non-portable inputs
Talk by Nelson Osacky from SoundCloud:
https://www.youtube.com/watch?v=tHe4IC0fBKg
org.gradle.parallel = true or --parallel
for decoupled modules


all the builds of your organization
searchable
powerful dashboards
build comparisons
enterprise grade remote cache
supports Gradle and Maven
installed on premise within your organization
know your build
monitor and measure
avoid unnecessary configuration
re-use task outputs