Gradle - 什么是非零退出值,我该如何解决?
我正在开发一个 Android 应用程序,每次运行时都会收到以下消息:
:module:someTask FAILEDFAILURE:构建失败并出现异常.* 什么地方出了错:任务 ':module:someTask' 执行失败.>这里有一些消息...以非零退出值 X 结束* 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪.使用 --info 或 --debug 选项运行以获得更多日志输出.构建失败总时间:Y.ZZ 秒
我已经尝试清理和构建项目,但错误仍然存在.
我看到了启用 Multidex 或增加堆大小的答案,但我确信我不需要任何一种解决方案.
我能做些什么来解决这个问题?
<小时>关于这个问题:这是
2
&3
- 这里的许多答案告诉你为每个模块打开
Tasks
文件夹并执行以下一些组合.- 要清理和重置生成文件的代码,请使用
build
>clean
后跟build
>build
. - 要检查嵌套的依赖关系,请使用
help
>dependencies
.确保没有重复. - 要检查代码中的语法错误和警告,请运行
verification
>lint
.这将输出一个 HTML 文件,您可以在浏览器中阅读该文件.Gradle 日志会显示Wrote HTML report to file:///path/to/app/build/outputs/lint-results.html
,因此只需打开该文件即可查看所有错误和警告. - 要尝试在设备上运行应用,请使用
install
>installDebug
.
补充说明
刚安装 Android Studio 时看到很多值为 2 的帖子,但出现错误
<块引用>'android-studio/jre/bin/java' 以非零退出值 2 结束
安装 Java JDK 并正确配置 Android Studio 以使用 JDK 似乎可以解决此问题.
<小时>如果您通过
<小时>compile 'com.google.android.gms:play-services:XYZ'
使用 Google Play 服务,则只需 包括您实际需要的依赖项,否则您很可能确实达到了 Multidex 限制.了解如何启用它.如果你的 Gradle 文件中有一行读取
compile fileTree(dir: 'libs', include: ['*.jar'])
,那么你不需要任何其他行有compile files('libs/some_file.jar')
因为第一种方式说在libs/
目录中包含 每个 JAR 文件."除此之外,如果您使用 Gradle 并且能够通过搜索 找到您想要使用的依赖项Maven Repository,那么强烈建议使用它而不是手动将 JAR 文件放入
<小时>libs/
目录.对于该站点上的每个库,都有一个 Gradle 选项卡,您只需复制该行并将compile <line youcopy>
放入dependencies
部分.如果您有一行编译另一个项目,例如
<小时>compile project(":project_name")
,那么请确保您没有从那里复制依赖项.另一种解决内存相关问题的方法是扩大堆大小,这是从
build.gradle
中完成的,就像这样<代码>android {//其他东西dex 选项 {javaMaxHeapSize "2g"//或 "4g" 如果你的设备有足够的内存}}
I am developing an Android application, and every time I run it, I get this message:
:module:someTask FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':module:someTask'. > some message here... finished with non-zero exit value X * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: Y.ZZ secs
I have already tried cleaning and building the project, but the error persists.
I have seen answers that say to enable Multidex or increase the heap size, but I am sure I don't need either solution.
What can I do to solve this?
About this question: This is a direct extension of What is a stack trace, and how can I use it to debug my application errors? except you are looking at a Gradle stack trace instead of a Java stack trace.
解决方案Foreword
That error message is not enough information to diagnose the problem. There are ways to get more information, and that should be inspected first.
The Gradle output itself should point at the actual error in the few lines above that message between
:module:someTask FAILED
and the last:module:someOtherTask
. Therefore, if you ask a question about your error, please edit your questions to include more context to the error.The problem
The
someTask
part of theapp:someTask FAILED
is very important as it tells you exactly which step of the build process has crashed.These steps include preparing dependencies, generating and merging the assets and resource files, checking the code for errors and compiling, then finally installing the app.
If at any point of the build process Gradle detects an anomaly, it will throw a non-zero exit value indicating an error has occurred.
The exit value itself is somewhat important.
1
is a just a general error code and the error is likely in the Gradle output2
seems to be related to overlapping dependencies or project misconfiguration.3
seems to be from including too many dependencies, or a memory issue.
There are probably others, so please feel free to provide your own comments or answers with other values.
The solution
The general solutions for the above (after attempting a Clean and Rebuild of the project) are:
1
- Address the error that is mentioned. Generally, this is a compile-time error, meaning some piece of code in your project is not valid. This includes both XML and Java for an Android project. Refer to the image for all the things going into the app
2
&3
- Many answers here tell you to enable multidex. While it may fix the problem, it is most likely a workaround. If you don't understand why you are using it (see the link), you probably don't need it.
If you are unable to find any error output in the Gradle log, then the recommended course of action would be to open Gradle window of Android Studio.
Open up the
Tasks
folder for each module and perform some combination of the following.- To clean and reset the code of generated files, use
build
>clean
followed bybuild
>build
. - To inspect nested dependencies, use
help
>dependencies
. Make sure none are duplicated. - To check your code for syntax errors and warnings, run
verification
>lint
. This will output an HTML file that you can read in your browser. The Gradle logs will sayWrote HTML report to file:///path/to/app/build/outputs/lint-results.html
, so just open that file to see all the errors and warnings. - To try and run the app on a device, use
install
>installDebug
.
Additional notes
I've seen many post with value 2 when just installing Android Studio and there is an error
'android-studio/jre/bin/java' finished with non-zero exit value 2
Installing the Java JDK and correctly configuring Android Studio to use the JDK seems to fix this issue.
If you are using the Google Play Services by
compile 'com.google.android.gms:play-services:X.Y.Z'
, then only include the dependencies you actually need, otherwise you most likely did hit the Multidex limit. See how to enable it.
If you have a line in your Gradle file that reads
compile fileTree(dir: 'libs', include: ['*.jar'])
, then you don't need any other line that hascompile files('libs/some_file.jar')
because that first way says "include every JAR file in thelibs/
directory."Along with that point, if you are using Gradle and are able to find the dependencies that you would like to use by searching the Maven Repository, then it is strongly encouraged to use that instead of manually placing JAR files into the
libs/
directory. For each library on that site, there is a Gradle tab and you just need to copy that one line and putcompile <line you copied>
into thedependencies
section.
If you have a line that compiles another project such as
compile project(":project_name")
, then make sure you aren't duplicating dependencies from there.
Another solution for memory-related issues involves expanding the heap size, which is done from the
build.gradle
like soandroid { // Other stuffs dexOptions { javaMaxHeapSize "2g" // or "4g" if your device has enough memory } }
- 要清理和重置生成文件的代码,请使用
相关文章