I have a multi-project (~10 modules) of which building takes about 20-30 seconds each time. When I press Run in Android Studio, I have to wait every time to rebuild the app,
The accepted answer is for older versions of android studio and most of them works still now. Updating android studio made it a little bit faster. Don't bother to specify heap size as it'll increase automatically with the increase of Xms and Xmx. Here's some modification with the VMoptions
In bin folder there's a studio.vmoptions file to set the environment configuration. In my case this is studio64.vmoptions Add the following lines if they're not added already and save the file. In my case I've 8GB RAM.
-Xms4096m
-Xmx4096m
-XX:MaxPermSize=2048m
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled
-XX:+HeapDumpOnOutOfMemoryError
-Dfile.encoding=utf-8`
Start android studio. Go to File-> Settings-> Build, Execution, Deployment-> Compiler
In case of using mac, at first I couldn't find the vmoptions. Anyway, here's a nice article about how we can change the vmoptions in MAC OSX. Quoting from this article here.
Open your terminal and put this command to open the vmoptions in MAC OSX:
open -e /Applications/Android\ Studio.app/Contents/bin/studio.vmoptions