Gradle - What Is a Non-Zero Exit Value and How to Fix It

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

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

Gradle finished with non-zero exit value 255

Generally, non-zero exit code means an irregular exit.
You should check out latest.log or debug.log in the running directory to inspect what was happened. They contain a crash report which helps identify your problem.

Gradle (Java finished with non-zero exit value 2)

This problem can be solved either by cleaning the project, and removing some unused libraries and methods from dependencies in build.gradle, OR by adding multidex support.

    defaultConfig {        
// Enabling multidex support.
multiDexEnabled true
minSdkVersion 13
targetSdkVersion 23
versionCode 1
versionName "1.0"
}

Java finished with non-zero exit value 2 - Android Gradle

I didn't know (by then) that "compile fileTree(dir: 'libs', include: ['*.jar'])" compile all that has jar extension on libs folder, so i just comment (or delete) this lines:

//compile 'com.squareup.retrofit:retrofit:1.9.0'
//compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
//compile 'com.squareup.okhttp:okhttp:2.2.0'

//compile files('libs/spotify-web-api-android-master-0.1.0.jar')
//compile files('libs/okio-1.3.0.jar')

and it works fine. Thanks anyway! My bad.

Android java.exe finished with non-zero exit value 1

It must have been a problem with my Java install. I removed all traces of Java (C://program files/Java/jdk) and jre folders and reinstalled it from the official page and now it works fine.



Related Topics



Leave a reply



Submit