如何修复 Android Studio 在执行 Gradle 任务时卡住的问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25629368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:35:31  来源:igfitidea点击:

How to fix Android Studio getting stuck executing Gradle tasks?

androidandroid-studio

提问by Oleksiy

Android Studio (Beta) 0.8.6 gets stuck when I try to build/run the project, as well as clean it. It stays responsive, but the Gradle: Executing Tasks message doesn't disappear (longer than 20 minutes is when I give up) and the app never runs.

当我尝试构建/运行项目以及清理它时,Android Studio (Beta) 0.8.6 卡住了。它保持响应,但 Gradle: Executing Tasks 消息不会消失(超过 20 分钟是我放弃的时候)并且应用程序永远不会运行。

This is what I attempted:

这是我尝试的:

  • Invalidate caches/restart

  • Force quit and restart

  • Remove it from Applications (on Mac) and re-download it from the official website.

  • 使缓存无效/重新启动

  • 强制退出并重新启动

  • 从应用程序(在 Mac 上)中删除它并从官方网站重新下载。

enter image description here

在此处输入图片说明

采纳答案by Oleksiy

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.

通过转到Android Studio-> Preferences-> Gradle-> 并勾选来修复它Offline work。仍然不知道出了什么问题,但至少现在它可以编译。

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

编辑:在新的 Android Studio 版本中,路径是File-> Other Settings-> Default Settings-> Build-Excecution-Deployment->Gradle

回答by tuanna

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.1is missing. So i installed it, worked like charm.

全新安装 Ubuntu 14.04之后,我最近刚刚遇到了这个问题。在谷歌搜索了一段时间但没有运气后,我检查了终端,结果发现libz.so.1丢失了。所以我安装了它,像魅力一样工作。

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

此处安装说明: libz.so.1:无法打开共享对象文件

回答by Biswajit Karmakar

Enable Offline Work from Setting

从设置启用离线工作

enter image description here

在此处输入图片说明

回答by HsnVahedi

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

您必须在文件中添加执行模式:AndroidStudioProjects/YourProjectName/gradlew,以便 Android Studio 可以执行它。如果您使用的是 linux 机器,请使用以下命令: chmod +x gradlew 来运行此命令,您可能需要具有管理权限。在 Ubuntu 中,您可以使用 sudo chmod +x gradlew 来做到这一点

回答by tir38

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.

  1. 从命令行运行 gradle 命令。它仍然失败吗?

    $ ./gradlew myTask

  2. 如果是这样,请使用调试标志重新运行。有什么有用的信息吗?

    $ ./gradlew myTask --debug

  3. 如果它只是挂起,请尝试获取堆栈跟踪。在转储文件中搜索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. 如果它只是在运行 Android Studio 时挂起,请在此处为 Android Studio 生成线程转储阅读更多信息

回答by a.p.

After a lot of struggle and following all solutions posted above (and in similar posts) I finally found that the problem on my PC was the antivirus application. I disabled it and gradle refreshed and built fast, as it did before the problem started.

经过大量努力并遵循上面(以及类似帖子中)发布的所有解决方案,我终于发现我的 PC 上的问题是防病毒应用程序。我禁用了它,并且 gradle 刷新并快速构建,就像在问题开始之前一样。

It looks like my antivirus recently started blocking gradle from updating/downloading necessary information from the Internet and that is what is causing the delays and eventually timeouts in loading and refreshing a project. Disabling the antivirus it corrects the problem, every time.

看起来我的防病毒软件最近开始阻止 gradle 从 Internet 更新/下载必要的信息,这就是导致加载和刷新项目延迟并最终超时的原因。每次禁用防病毒软件都会纠正问题。

回答by Alex

In my case looks like gradle was missing 32 bit library on a 64 bit Ubuntu 16.04. Doing sudo apt-get install libstdc++6:i386resolved the problem.

在我的情况下,看起来 gradle 在 64 位 Ubuntu 16.04 上缺少 32 位库。做sudo apt-get install libstdc++6:i386解决了问题。

回答by nurettin

This happened to me after adding google play api to dependencies{}. ./gradlew app:assembleDebugshowed an error saying something like Dex ID limit reached use multi-dex.just as I was setting it up as described here, I decided to remove the dependency I recently added and the issue was gone.

在将 google play api 添加到依赖项后,这发生在我身上{}。./gradlew app:assembleDebug显示一个错误,说明就像Dex ID limit reached use multi-dex.我按照此处所述进行设置一样,我决定删除我最近添加的依赖项,问题就消失了。

Google also suggests using proguard as a technique to optimize the number of Dex IDs in the same document.

Google 还建议使用 proguard 作为优化同一文档中 Dex ID 数量的技术。

回答by Ali Zeynali

For GNU/Linux Users:

对于 GNU/Linux 用户:

I copied the SDK from another storage to my home directory. Then I was getting stock gradle build running. The problem was the permissions of SDK files. Watch the gradle console log and if you see some permission denieded stuff, then do the following - give all the files executing permission by this command:

我将 SDK 从另一个存储复制到我的主目录。然后我得到了股票gradle build running。问题是 SDK 文件的权限。查看 gradle 控制台日志,如果您看到一些权限被拒绝的内容,请执行以下操作 - 通过此命令授予所有文件执行权限:

chmod -R +x /path/to/android-sdk

chmod -R +x /path/to/android-sdk

回答by brunorcabral

In my case, I was getting this error by the Gradle Builder (Android Studio 2.2, Windows 10)

就我而言,Gradle Builder(Android Studio 2.2,Windows 10)出现此错误

java.lang.RuntimeException: Timed out while waiting for slave aapt process, try setting environment variable SLAVE_AAPT_TIMEOUTto a value bigger than 5 seconds

java.lang.RuntimeException: 在等待从属 aapt 进程时超时,尝试将环境变量设置SLAVE_AAPT_TIMEOUT为大于5 秒的值

The solution was just to add the variable SLAVE_AAPT_TIMEOUT to my Windows environment, and set it to 30

解决方案只是将变量 SLAVE_AAPT_TIMEOUT 添加到我的 Windows 环境中,并将其设置为 30