Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

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

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

gradle process

  • 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.

Android Studio Gradle

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...