Accurate performance tips for Gradle & Android Studio
There’s a lot out there on how to improve Android Studio / Gradle performance. After trying out everything using a consistent method, I can now sum up the things that work:
1. Upgrade Java, Gradle and the gradle-android-plugin
Keep everything upgraded to the latest versions. I have the gradle wrapper in every project, but that’s up to you. The gradle-daemon is enabled by default in latest gradle.
2. Sensible JVM options for Android Studio and Gradle
I have tried out many JVM-settings, and those were the best allround-fit. Others made the build slower or made no difference.
-Xmx3g
maximum heap: 3G (2G if you have less than 8G of RAM)
Settings like -XX:ReservedCodeCacheSize
or -XX:UseCompressedOops
are enabled by default in Java-8, therefore not necessary. You see the default-flags using this command:
java -XX:+PrintFlagsFinal -version
Global Gradle JVM-options
The following line goes in your ~/.gradle/gradle.properties
org.gradle.jvmargs=-Xmx3g
Android Studio JVM-options
Same goes in your ~/Library/Preferences/AndroidStudio<Version>/studio.vmoptions
-Xmx3g
3. Important: make Android Studio use the same Gradle daemon you use on the commandline
Gradle’s daemon is enabled by default since 3.0
.
But Android Studio keeps firing up it’s own daemon, because it uses it’s own JDK.
Therefore, if you build on the command-line as well, you often have two daemons running, each swallowing gigabytes of RAM. If you use Kotlin, you have additional compiler-daemons running as well, which make it worse.
To remedy this, set the JDK location of Android Studio project to the globally installed JDK:
Final Testing
Check your new settings:
- Close Android Studio
- Kill all Gradle-daemons:
./gradlew — stop
- Clean the project:
./gradlew clean
(this will start the Gradle-daemon) - Build:
./gradlew assembleDebug
(this will start the kotlin-daemon) - Clean again:
./gradlew clean
- Build:
./gradlew assembleDebug --profile
. If you are still unhappy with your build-time check out the generated profile. If your kotlin takes ages to build, there’s a chance that the DNS-configuration of your machine has a problem. - Start Android Studio
- Clean the project, make sure it doesn’t start a new daemon by checking your Activity Monitor and the log.
- Build the project
Afterword
There are many other settings out there, e.g. setting the build-process to offline, fiddle around with dexing etc. I tried ALL of them and repeated the clean-build-test-process above to get consistent measurements.
If you have any tips that improve on this, when measured using the process above, tell me.
Happy, hopefully a little faster, hacking.