How to fix Android Studio getting stuck executing Gradle tasks?

The question seems old but in case anyone need it.

I have just faced this problem recently, after a fresh Ubuntu 14.04 installation. After google for a while with no luck i checked the terminal, it turned out that libz.so.1 is missing. So i installed it, worked like charm.

Installing instruction here: libz.so.1: cannot open shared object file


Fixed it by going to Android Studio -> Preferences -> Gradle -> and ticking Offline work. Still have no idea what was wrong, but at least now it compiles.

Edit: In new Android Studio Versions the path is File -> Other Settings -> Default Settings -> Build-Excecution-Deployment -> Gradle