How to fix Android Studio getting stuck executing Gradle tasks?

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


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


Enable Offline Work from Setting

enter image description here


This can happen for lots of reasons. So instead of giving an exact fix, here are some steps to help isolate the cause.

  1. Run the gradle command from command line. Does it still fail?

    $ ./gradlew myTask

  2. If so, re-run with debug flag. Any useful info?

    $ ./gradlew myTask --debug

  3. If it's just hanging, try getting a stack trace. Search the dump file for myTask

    $ jps -mv | grep Gradle  <--- be sure to use capital "G"
    2290 GradleDaemon 3.3 -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Xmx2304M -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant
    $ jstack -l 2290 >> dump.txt <--- your process ID will be different
    
  4. If it's only hanging when running Android Studio, generate thread dump for Android Studio read more here.


You have to add execution mode to the file: AndroidStudioProjects/YourProjectName/gradlew so that Android Studio can execute it. if you are on a linux machine use this command: chmod +x gradlew to run this command you may need to have administration privileges. in Ubuntu you can use sudo chmod +x gradlew to do that