how to improve gradle build speed
TRANSCRIPT
![Page 1: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/1.jpg)
How to improve Gradle build speed
Fate Chang2016/05/18
Google I/O Extended 2016 Taipei
![Page 2: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/2.jpg)
About me
● Fate Chang● Leopard Mobile● Android developer● Focus on architecture and performance● 1000th member of GCPUG.TW● fb.me/fate.tw● +FateChang
![Page 3: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/3.jpg)
When build is so slow...It’s easily distracted.
![Page 4: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/4.jpg)
Why build so slow
● Slow computer● Not enough memory● Complicate project● Old version tools● Wrong setting● Slow network● ...
![Page 5: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/5.jpg)
We got a chance to try it● Try our Gradle configuration on Mac Pro● Our project CMSecurity release build time around 2 mins.● Can we make it faster?● At this moment Gradle daemon is all I know
![Page 6: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/6.jpg)
Let’s Do It!Step by step
![Page 7: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/7.jpg)
Profiling
● Example project : https://github.com/google/iosched● Checkout from github● Setup baseline
○ $ ./gradlew assembleDebug --dry-run○ Run above command several time ○ Get average time○ Total time: 6.371 secs
![Page 8: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/8.jpg)
--dry-run
● Runs the build with all task actions disabled○ Sometimes you are interested in which tasks are executed in which order
for a given set of tasks specified on the command line, but you don't want the tasks to be executed.
![Page 9: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/9.jpg)
--profile
● $ ./gradlew assembleDebug --dry-run --profile○ --profile parameter will generate profile report page○ Total build time : 7.809s
● $ open build/reports/profile/profile-2016-05-18-15-18-07.html
![Page 10: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/10.jpg)
--configure-on-demand
● $ ./gradlew assembleDebug --dry-run --profile --configure-on-demand
● Total build time : 6.704s
![Page 11: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/11.jpg)
--daemon
● $ ./gradlew assembleDebug --dry-run --profile --configure-on-demand --daemon
● Total build time : 2.663s
![Page 12: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/12.jpg)
Notes of using Gradle daemon
DON’T USE GRADLE DAEMON ON CI.When should I not use the Gradle Daemon
https://docs.gradle.org/current/userguide/gradle_daemon.html#when_should_i_not_use_the_gradle_daemon
How does the Gradle Daemon make builds faster
https://docs.gradle.org/current/userguide/gradle_daemon.html#N1056F
![Page 13: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/13.jpg)
--parallel
● $ ./gradlew assembleDebug --dry-run --profile --configure-on-demand --daemon --parallel
● Total build time : 2.382s
![Page 14: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/14.jpg)
The improvement7.832s -> 2.382s
![Page 15: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/15.jpg)
Other useful Gradle parameter
● --info○ Output more log during build.
● --offline○ Set Gradle to operate without accessing network
resources.○ Note : should remove --offline to check consistency
when library upgraded.● More docs : https://docs.gradle.
org/current/userguide/gradle_command_line.html
![Page 16: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/16.jpg)
How to add parameters in Android Studio
![Page 17: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/17.jpg)
Optimizing Multidex Development Builds
Ref:https://developer.android.com/studio/build/multidex.html#dev-build
![Page 18: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/18.jpg)
Multidex enabled sample project result
Scenario: Clean project and rebuild.
$ ./gradlew clean
$ ./gradlew assembleXxxxx --configure-on-demand --daemon --parallel --offline --info
minSdkVersion 14
Total time: 63.168s secs
minSdkVersion 21
Total time: 52.029 secs
![Page 19: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/19.jpg)
That’s All?
![Page 20: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/20.jpg)
Most recent changes
● Newer Gradle version performance is better○ In 2.13 release notes, measured up to 25% improvements○ Ref : https://docs.gradle.org/2.13/release-notes
● Faster Android Studio Builds with Dex In Process○ Enabled in Android Studio 2.1○ Modify gradle.properties○ org.gradle.jvmargs=-Xmx2048m○ Ref : https://medium.com/google-developers/faster-android-studio-builds-
with-dex-in-process-5988ed8aa37e
![Page 21: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/21.jpg)
Update Gradle Android Plugin Version.
If you upgrade Android Studio to 2.1.x recently, you’ll see this dialog
Note: Update Gradle plugin up to 2.x, Intstant run is default enabled
![Page 22: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/22.jpg)
If yout don’t update in previous dialog, you can update configuration manually.
Update Gradle Android Plugin Version Manually In Android Studio
![Page 23: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/23.jpg)
Android Studio 2.1 new feature
● Dex In Process● This can dramatically increase the speed of
full clean build.● Should update Android plugin version to 2.1.0
as well.
![Page 24: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/24.jpg)
Make dex in process running
If you add dexOptions in build.gradle
You may see this message.
![Page 25: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/25.jpg)
Dex in process not working log
Build with --info parameter
If you see Dexing out-of-process log, it’s not working.
![Page 26: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/26.jpg)
Let’s add setting to gradle.properties
org.gradle.daemon=trueorg.gradle.configureondemand=trueorg.gradle.parallel=trueorg.gradle.jvmargs=-Xmx5120m
Don’t commit these config to version control.or yout can use -Dorg.gradle.jvmargs=-Xmx5120m parameter in command line
![Page 27: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/27.jpg)
Still not working?
Reduce javaMaxHeapSize
Build with --info parameter
If you see Dexing in-process log, you’re all set.
![Page 28: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/28.jpg)
Speed Up In CMSecurity
Build time from 2 mins 20s to less than 1 min. (Tested on MBPR 15” mid 2012 16GB Ram)
![Page 29: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/29.jpg)
Slow in Android Studio but fast in command line?
Try disable Instant Run.
![Page 30: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/30.jpg)
Summary
● Faster CPU is better!● Use latest gradle version (2.13)● Use latest android plugin (2.1.0)● Adjust setting according to your memory● Disable Instant Run, unless you really need it.
One setting won’t fit all, you need to try the combination for best result
Note : Use retrolambda will cause rebuild each time.
![Page 31: How to improve gradle build speed](https://reader033.vdocument.in/reader033/viewer/2022050613/589aa28c1a28abfc1a8b5929/html5/thumbnails/31.jpg)
References
● 6 tips to speed up your Gradle build○ https://medium.com/@shelajev/6-tips-to-speed-up-your-gradle-build-
3d98791d3df9
● Faster Android Studio Builds with Dex In Process○ https://medium.com/google-developers/faster-android-studio-builds-with-
dex-in-process-5988ed8aa37e