6 tips to speed up your Gradle build

Oleg Šelajev
Feb 18, 2016 · 10 min read

Speeding up Gradle builds

git clone http://github.com/google/iosched
cd ioshed
./gradlew :android:assembleDebug --dry-run
BUILD SUCCESSFUL

Total time: 8.674 secs
./gradlew :android:assembleDebug --dry-run --profile
open build/reports/profile/profile-2016-02-02-15-39-17.html

Use configure on demand

./gradlew :android:assembleDebug --dry-run --profile --configure-on-demand
BUILD SUCCESSFULTotal time: 7.144 secs
echo 'org.gradle.configureondemand=true' >> ~/.gradle/gradle.properties

Use Gradle daemon

./gradlew :android:assembleDebug --dry-run --no-daemon
# vs.
./gradlew :android:assembleDebug --dry-run --daemon
BUILD SUCCESSFULTotal time: 2.536 secs
echo 'org.gradle.daemon=true' >> ~/.gradle/gradle.properties

Use the latest Gradle version

> Failed to apply plugin [id 'com.android.application']
> Gradle version 2.2 is required. Current version is 2.10. If using the gradle wrapper, try editing the distributionUrl in /Users/shelajev/repo/tmp/iosched/gradle/wrapper/gradle-wrapper.properties to gradle-2.2-all.zip
BUILD SUCCESSFULTotal time: 1.356 secs

Optimize your project

Avoid heavy computations

def cmd = 'git rev-list HEAD --first-parent --count'
def gitVersion = cmd.execute().text.trim().toInteger()
android {
defaultConfig {
versionCode gitVersion
}
}
def gitVersion() {
if (!System.getenv('CI_BUILD')) {
// don't care
return 1
}
def cmd = 'git rev-list HEAD --first-parent --count'
cmd.execute().text.trim().toInteger()
}
android {
defaultConfig {
versionCode gitVersion()
}
}

Fix the dependencies

dependencies {
compile 'com.google.code.gson:gson:2.+'
}

Modularize the project and parallelize the build

echo 'org.gradle.parallel=true' >> ~/.gradle/gradle.properties

Conclusion

Oleg Šelajev

Written by

Developer advocate for GraalVM at OracleLabs. VirtualJUG organizer. Became Java Champion in 2017. Likes programming languages, compilers, performance, chess

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade