Gradle - What is a non-zero exit value and how do I fix it?

此生再无相见时 提交于 2019-11-26 11:54:04
cricket_007

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 
    }
}

A non-zero exit value is like a check engine light. It simply tells you that something went wrong. You still need to do further diagnostics to figure out what the problem is exactly. To start, you should scroll up in the output from Gradle and see if there is anything else that gives more specific details about what went wrong.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!