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

前端 未结 23 2441
甜味超标
甜味超标 2020-11-22 02:54

I\'m getting this error executing my Android app (I cleaned it and then built it, but the error is still present)

  • Sync: OK
  • Make Project: OK
相关标签:
23条回答
  • 2020-11-22 03:01

    I had the same error after converting my project to Kotlin. My problem was that my jre location was changed to an invalid path during the process (I wonder why this could happen... made me waste time). Fixed it by doing this:

    File > Project Structure > SDK Location 
    

    Unchecked the Use embedded JDK option, which was pointing to an old JDK installation, and selected the correct one:

    /home/my_user/jdk1.8.0_101
    

    After changing this, the error disappeared

    0 讨论(0)
  • 2020-11-22 03:02

    In my case, the problem was that the new library (gradle dependency) that I had added was relying on some other dependencies and two of those underlying dependencies were conflicting/clashing with some dependencies of other libraries/dependencies in my build script. Specifically, I added Apache Commons Validator (for email validation), and two of its dependencies (Apache Commons Logging and Apache Commons Collections) were conflicting with those used by Robolectric (because different versions of same libraries were present in my build path). So I excluded those conflicting versions of dependencies (modules) when adding the new dependency (the Validator):

    compile ('commons-validator:commons-validator:1.4.1') {
        exclude module: 'commons-logging'
        exclude module: 'commons-collections'
    }
    

    You can see the dependencies of your gradle module(s) (you might have only one module in your project) using the following gradle command (I use the gradle wrapper that gets created for you if you have created your project in Android Studio/Intellij Idea). Run this command in your project's root directory:

    ./gradlew :YOUR-MODULE-NAME:dependencies

    After adding those exclude directives and retrying to run, I got a duplicate file error for NOTICE.txtthat is used by some apache commons libraries like Logging and Collections. I had to exclude that text file when packaging. In my build.gradle, I added:

    packagingOptions {
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    

    It turned out that the new library (the Validator) can work with slightly older versions of its dependencies/modules (which are already imported into my build path by another library (Robolectric)). This was the case with this specific library, but other libraries might be using the latest API of the underlying dependencies (in which case you have to try to see if the other libraries that rely on the conflicting module/dependency are able to work with the newer version (by excluding the older version of the module/dependecy under those libraries's entries)).

    0 讨论(0)
  • 2020-11-22 03:02

    WORKED FOR ME :)

    i upgraded the java to the latest version 8 previously it was 7 and then go to OPEN MODULE SETTING right clicking on project and changed the jdk path to /usr/lib/jvm/java-8-oracle the new java 8 installed. And restart the studio

    check in /usr/lib/jvm for java 8 folder name

    I am using ubuntu

    0 讨论(0)
  • 2020-11-22 03:03

    Just in case if someone still struggling with this and have no clue why is this happening and how to fix. In fact this error

    Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdkx.x.x_xx\bin\java.exe'' finished with non-zero exit value 2

    can have many reasons to happen but certainly not something related to your JDK version so don't wast your time in wrong direction. These are two main reasons for this to happen

    1. You have same library or jar file included several places and some of them conflicting with each other.
    2. You are about to or already exceeded 65k method limit

    First case can be fixed as follows: Find out which dependencies you have included multiple times. In order to do this run following command in android studio terminal

    gradlew -q dependencies yourProjectName_usually_app:dependencies --configuration compile
    

    this will return all the dependencies but jar files that you include from lib folder try to get rid of duplication marked with asterisk (*), this is not always possible but in some cases you still can do it, after this try to exclude modules that are included many times, you can do it like this

    compile ('com.facebook.android:facebook-android-sdk:4.0.1'){
        exclude module: 'support-v4'
    }
    

    For the second case when you exceeding method limit suggestion is to try to minimize it by removing included libraries (note sounds like first solution) if no way to do it add multiDexEnabled true to your defaultConfig

    defaultConfig {
       ...
       ...
       multiDexEnabled true
    }
    

    this increases method limit but it is not the best thing to do because of possible performance issues IMPORTANT adding only multiDexEnabled true to defaultConfig is not enough in fact on all devices running android <5 Lollipop it will result in unexpected behavior and NoClassDefFoundError. how to solve it is described here

    0 讨论(0)
  • 2020-11-22 03:03

    In my case, I got this error when there are 2 or more libraries conflict (same library but different versions). Check your app build.gradle in dependencies block.

    0 讨论(0)
  • 2020-11-22 03:03

    My problem was that apart from having

    com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.X.X_XX.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

    I had this trace as well:

    Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/mypackage/ClassX;

    The problem was that I was adding the same class in two differents libraries. Removing the class/jar file from one of the libraries, the project run properly

    0 讨论(0)
提交回复
热议问题