Gradle - 什么是非零退出值,我该如何解决? [英] Gradle - What is a non-zero exit value and how do I fix it?

查看:302
本文介绍了Gradle - 什么是非零退出值,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 :module:someTask 

我正在开发一个Android应用程序,每次运行它时,都会收到以下消息:失败
失败:构建失败,出现异常。
*出错:
任务':module:someTask'的执行失败。
>这里有一些消息...用非零退出值X
完成*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。
BUILD FAILED
总时间:Y.ZZ秒

我已经尝试清理和建设项目,但错误仍然存​​在。



我已经看到了启用Multidex或增加堆大小的答案,但我确定我不需要这两种解决方案。



我能做些什么来解决这个问题?




关于这个问题:这是




  • 2 & 3 - 这里有很多答案告诉你:



    打开每个模块的任务文件夹并执行以下操作的一些组合。 build > clean 然后加上 build > build


  • 要检查嵌套依赖关系,请使用 help > 依赖关系。确保没有重复。
  • 要检查代码中的语法错误和警告,请运行验证> lint 。这将输出一个HTML文件,您可以在浏览器中阅读该文件。 Gradle日志会显示将HTML报告写入文件:///path/to/app/build/outputs/lint-results.html ,因此只需打开该文件即可看到所有的错误和警告。

  • 要在设备上尝试运行该应用程序,请使用 install > installDebug



其他注意事项



2安装Android Studio时出现错误


'android-studio / jre / bin / java'以非零退出值2


安装Java JDK并正确配置Android Studio以使用JDK似乎可以解决此问题。






如果您通过 compile'c​​om.google.android使用 Google Play服务。 gms:play-services:XYZ',那么只有包含您实际需要的依赖性,否则您很可能会碰到Multidex限制。了解如何启用






如果您的Gradle文件中有一行读取 compile fileTree(dir:'libs',include:['* .jar' ]),那么你不需要任何具有编译文件('libs / some_file.jar')的其他行,因为第一种方式在 libs / 目录中包含每个 JAR文件。



除此之外,如果您使用Gradle并能够通过搜索 Maven Repository ,强烈建议使用它,而不是手动将JAR文件放入 libs / 目录。对于该网站上的每个库,都有一个Gradle选项卡,您只需要复制该行并将 compile&line line复制> 放入依赖关系部分。



如果你有一行编译另一个项目,如 compile project(:project_name ),然后确保你没有从那里复制依赖关系。




与内存相关的问题的另一个解决方案涉及扩展堆大小,这是从 build.gradle 像这样

  android {
//其他东西
dexOptions {
javaMaxHeapSize2g//如果你的设备有足够的内存

$ / code $ <


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 the app: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 output
  • 2 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 by build > 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 say Wrote 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 has compile files('libs/some_file.jar') because that first way says "include every JAR file in the libs/ 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 put compile <line you copied> into the dependencies 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 so

android {
    // Other stuffs
    dexOptions {
        javaMaxHeapSize "2g" // or "4g" if your device has enough memory 
    }
}

这篇关于Gradle - 什么是非零退出值,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆