Android Studio 构建失败,并显示“在根项目‘MyProject’中找不到任务‘’。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25172006/
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
Android Studio build fails with "Task '' not found in root project 'MyProject'."
提问by TomRichardson
I get this error when trying to build my project after changing laptop and updating to Android Studio version 0.8.2.
在更改笔记本电脑并更新到 Android Studio 版本 0.8.2 后尝试构建我的项目时出现此错误。
FAILURE: Build failed with an exception.
What went wrong: Task '' not found in root project 'MyProject'.
Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
FAILURE:构建失败,出现异常。
出了什么问题:在根项目“MyProject”中找不到任务“”。
尝试:运行 gradle tasks 以获取可用任务的列表。使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。
构建失败
Here are my Gradle files:
这是我的 Gradle 文件:
Top Level settings.gradle
顶级 settings.gradle
include ':MyProject'
build.gradle in MyProject:
我的项目中的 build.gradle:
apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:0.12.+"
}
}
repositories {
mavenCentral()
}
android {
compileSdkVersion 20
buildToolsVersion "20"
defaultConfig {
minSdkVersion 14
targetSdkVersion 20
}
}
dependencies {
compile 'de.timroes.android:EnhancedListView:0.3.0@aar'
compile 'com.nineoldandroids:library:2.4.0'
}
Top level build.gradle is empty
顶级 build.gradle 为空
回答by ????bρ??
Though late in answering, this is a hard one to Google (the single quotes) and it's not clear what's happening. I don't have the reputation yet to comment or ask for scope (or post 3 links), so this answer may be a little tedious.
虽然回答晚了,但这对谷歌来说很难(单引号),目前还不清楚发生了什么。我还没有评论或要求范围(或发布 3 个链接)的声誉,所以这个答案可能有点乏味。
To answer fast, you may have multiple Gradle plugins in your project.
为了快速回答,您的项目中可能有多个 Gradle 插件。
Synchronize Gradle Wrapper and Plugins
同步 Gradle 包装器和插件
My issue seemed to start with a corrupted IML file. Android Studio (between closing and reopening a project) began complaining an IML was gone (it wasn't) and a module should be deleted, which I declined. It persisted, I upgraded to AS 0.8.7 (canary channel) and got stuck on the OP issue (Task '' not found in root project). This completely blocked builds so I had to dig in to Gradle.
我的问题似乎始于损坏的 IML 文件。Android Studio(在关闭和重新打开项目之间)开始抱怨 IML 消失了(它没有)并且应该删除一个模块,我拒绝了。它仍然存在,我升级到 AS 0.8.7(金丝雀频道)并陷入了 OP 问题(在根项目中找不到任务“”)。这完全阻止了构建,所以我不得不深入研究 Gradle。
My repair steps on OSX (please adjust for Windows):
我在 OSX 上的修复步骤(请针对 Windows 进行调整):
- Upgrade Android Studio to 0.8.7
- Preferences | Updates | Switch "Beta Channel" to "Canary Channel", then do a Check Now.
- You might be able to skip this.
Checked the Gradle wrapper (currently 1.12.2; don't try to use 2.0 at this time).
Assuming you don't need a particular version, use the latest supported distribution
$ vi ~/project/gradle-wrapper.properties ... distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
This can be set in Android Studio at Preferences | Gradle (but 0.8.7 was giving me ‘invalid location' errors).
- The 'wrapper' is just a copy of Gradle for each Android Studio project. It allows you to have Gradle 2 in your OS, and different versions in your projects. The Android Developer docs explain that here.
- Then adjust your build.gradle files for the plugin. The Gradle plugin version must be compatible with the distribution/wrapper version, for the whole project. As the Tools documentation (tools.android.com/tech-docs/new-build-system/user-guide#TOC-Requirements) is slightly out of date, you can set the plugin version too low (like 0.8.0) and Android Studio will throw an error with the acceptable range for the wrapper.
- 将 Android Studio 升级到 0.8.7
- 偏好 | 更新 | 将“Beta 频道”切换到“Canary 频道”,然后立即检查。
- 你也许可以跳过这个。
检查 Gradle 包装器(当前为 1.12.2;此时不要尝试使用 2.0)。
假设您不需要特定版本,请使用最新的受支持发行版
$ vi ~/project/gradle-wrapper.properties ... distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
这可以在 Android Studio 中的 Preferences | 中设置。Gradle(但 0.8.7 给了我“无效位置”错误)。
- “包装器”只是每个 Android Studio 项目的 Gradle 副本。它允许您在操作系统中使用 Gradle 2,并在您的项目中使用不同的版本。Android 开发人员文档在此处解释了这一点。
- 然后调整插件的build.gradle 文件。对于整个项目,Gradle 插件版本必须与发行版/包装器版本兼容。由于工具文档 (tools.android.com/tech-docs/new-build-system/user-guide#TOC-Requirements) 有点过时,您可以将插件版本设置得太低(如 0.8.0)和Android Studio 将在可接受的包装器范围内引发错误。
Example, in build.gradle, you have this plugin:
例如,在 build.gradle 中,你有这个插件:
dependencies {
classpath "com.android.tools.build:gradle:0.12.+"
}
You can try switching it to the exact version, like this:
您可以尝试将其切换到确切版本,如下所示:
dependencies {
classpath "com.android.tools.build:gradle:0.12.2"
}
and (after recording what version you're changing from in each case) verifying that every build.gradle file in your project pulls in the same plugin version. Keeping the “+” should work (for 0.12.0, 0.12.1, 0.12.2, etc), but my build succeeded when I updated Google's Volley library (originally gradle:0.8.+) and my main project (originally 0.12.+) to the fixed version: gradle:0.12.2.
并且(在记录您在每种情况下更改的版本之后)验证您项目中的每个 build.gradle 文件都拉入相同的插件版本。保留“+”应该可以工作(对于 0.12.0、0.12.1、0.12.2 等),但是当我更新 Google 的 Volley 库(最初是 gradle:0.8.+)和我的主要项目(最初是 0.12.2.1)时,我的构建成功了。 +) 到固定版本:gradle:0.12.2。
Other checks
其他检查
Ensure you don't have two Android Application modules in the same Project
This may interact with the final solution (different Gradle versions, above), and cause
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define (various classes)
To check, Build | Make Project should not pop up a window asking what application you want to make.
- Invalidate your caches
- File | Invalidate Caches / Restart (stackoverflow.com/a/19223269/513413)
- If step 2 doesn't work, delete ~/.gradle/ (www.wuttech.com/index.php/tag/groovy-lang-closure/)
- Quit Android Studio
- $ rm -rf ~/.gradle/
- Start Android Studio, then sync:
- Tools | Android | Sync Project with Gradle Files
- Repeat this entire sequence (quit...sync) a few times before giving up.
- Clean the project
- Build | Clean Project
确保在同一个项目中没有两个 Android 应用程序模块
这可能会与最终解决方案(不同的 Gradle 版本,以上)交互,并导致
UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define (various classes)
要检查,构建 | Make Project 不应弹出询问您要制作什么应用程序的窗口。
- 使您的缓存无效
- 档案 | 使缓存无效/重新启动 (stackoverflow.com/a/19223269/513413)
- 如果第 2 步不起作用,请删除 ~/.gradle/ (www.wuttech.com/index.php/tag/groovy-lang-closure/)
- 退出 Android Studio
- $ rm -rf ~/.gradle/
- 启动 Android Studio,然后同步:
- 工具 | 安卓 | 将项目与 Gradle 文件同步
- 在放弃之前重复整个序列(退出...同步)几次。
- 清理项目
- 构建 | 清洁工程
If You See This...
如果你看到这个...
In my recent builds, I kept seeing horrible fails (pages of exceptions) but within seconds the messages would clear, build succeeded and the app deployed. Since I could never explain it and the app worked, I never noticed that I had two Gradle plugins in my project. So I think the Gradle plugins fought each other; one crashed, the other lost its state and reported the error.
在我最近的构建中,我一直看到可怕的失败(异常页面),但在几秒钟内消息就会清除,构建成功并部署了应用程序。由于我永远无法解释它并且该应用程序可以运行,因此我从未注意到我的项目中有两个 Gradle 插件。所以我认为 Gradle 插件互相争斗;一个崩溃了,另一个失去了状态并报告了错误。
If you have time, the 1-hour video "A Gentle Introduction to Gradle" (www.youtube.com/watch?v=OFUEb7pLLXw) really helped me approach the Gradle build files, tasks, build decisions, etc.
如果你有时间,1 小时的视频“A Gentle Introduction to Gradle”(www.youtube.com/watch?v=OFUEb7pLLXw)真的帮助我处理了 Gradle 构建文件、任务、构建决策等。
Disclaimer
免责声明
I'm learning this entire stack, on a foreign OS, after working a different career...all at the same time and under pressure. In the last few months I have hit every wall I think Android has; I've been here quite often and this is my first post. I thought this was a hard fix, so I sincerely apologize if the quality of my answer reflects the difficulty I had in getting to it.
在从事不同的职业之后,我正在外国操作系统上学习整个堆栈……所有这些都是在压力下同时进行的。在过去的几个月里,我遇到了我认为 Android 遇到的每一面墙;我经常来这里,这是我的第一篇文章。我认为这是一个很难解决的问题,所以如果我的回答的质量反映了我在解决它时遇到的困难,我真诚地道歉。
回答by Ankit Bansal
Download system image(of targeted android level of your project) from sdk manager for the project you have imported.
从您导入的项目的 sdk 管理器下载系统映像(项目的目标 android 级别)。
This error comes when you do not have target sdk of your android project installed in sdk folder
当您没有在 sdk 文件夹中安装 android 项目的目标 sdk 时会出现此错误
for eg. Your imported project's target sdk level may be android-19 and on sdk folder->system-images you may have android-21 installed. so you have to download android-19 system image and other files from sdk manager or you can copy paste it if you have the system image.
例如。您导入的项目的目标 sdk 级别可能是 android-19,并且在 sdk 文件夹->system-images 上,您可能安装了 android-21。所以你必须从sdk manager下载android-19系统镜像和其他文件,或者如果你有系统镜像,你可以复制粘贴。
回答by toni
Remove:
消除:
<facet type="android" name="Android">
<configuration />
</facet>
in your iml file. That works for me.
在您的 iml 文件中。这对我行得通。
回答by rehan
This happened to me recently when I close one Android Studio project and imported another Eclipse project. It seemed to be some bug in Android Studio where it preserves some gradle settings from previously open project and then get confused in the new project.
最近,当我关闭一个 Android Studio 项目并导入另一个 Eclipse 项目时,这发生在我身上。这似乎是 Android Studio 中的一些错误,它保留了以前打开的项目中的一些 gradle 设置,然后在新项目中感到困惑。
The solution was extremely simple: Close the project and shut down Android Studio completely, before re-opening it and then import/open the new project. Everything goes smoothly from then on.
解决方案非常简单:关闭项目并完全关闭 Android Studio,然后重新打开它,然后导入/打开新项目。从此一切顺利。
回答by holangun
Simple fix for me was
对我来说简单的修复是
- Build > Clean project
- Restart Android Studio
- 构建 > 清理项目
- 重启安卓工作室
回答by Josh
I got this error when switching from one Git branch to another, and then trying to run "Clean Project". I used ack
to search for the Task name, and found it in a .iml file.
从一个 Git 分支切换到另一个 Git 分支,然后尝试运行“Clean Project”时出现此错误。我曾经ack
搜索任务名称,并在 .iml 文件中找到它。
My solution was to regenerate the project's .iml file by clicking (in the main menu) Tools > Android > Sync Project with Gradle Files.(Thanks to this answer.)
我的解决方案是通过单击(在主菜单中)Tools > Android > Sync Project with Gradle Files来重新生成项目的 .iml 文件。(感谢这个答案。)
回答by David
Apparently this error has multiple causes. Here's what fixed it for me.
显然这个错误有多种原因。这是为我解决的问题。
I was running the build command like this:
我正在运行这样的构建命令:
./gradlew :testapp: build
Running it without the space fixed the issue:
在没有空间的情况下运行它解决了这个问题:
./gradlew :testapp:build
回答by somesh
In my case, setting the 'Gradle version' same as the 'Android Plugin version' under File->Project Structure->Project fixed the issue for me.
就我而言,在 File->Project Structure->Project 下将“Gradle 版本”设置为与“Android 插件版本”相同为我解决了这个问题。
回答by Mohammed Mukhtar
Sometimes, if you have opened two windows of Android Studio, and when you try to compile, this issue might happen. For me, when I was compiling a backed Google Cloud Endpoint module which was not embedded in a project, rather shared among different Android Studio projects, and when there is more than once instance open, this error use to spring up for me. But as soon as you close other windows, everything will be fine. Sometimes, you might have to restart Android Studio altogether.
有时,如果您打开了 Android Studio 的两个窗口,并且在尝试编译时,可能会出现此问题。对我来说,当我编译一个支持的 Google Cloud Endpoint 模块时,该模块没有嵌入到项目中,而是在不同的 Android Studio 项目之间共享,并且当打开多个实例时,这个错误就会出现在我身上。但是一旦关闭其他窗口,一切都会好起来的。有时,您可能必须完全重新启动 Android Studio。
回答by Morten Holmgaard
I got this problem because it could not find the Android SDK path. I was missing a local.properties
file with it or an ANDROID_HOME
environment variable with it.
我遇到这个问题是因为它找不到 Android SDK 路径。我丢失了一个local.properties
文件或一个ANDROID_HOME
环境变量。