Dedicated Server

finished with non-zero exit value 2 : How to handle duplicate dependencies error in android ?

Here is the detailed view of the gradle sync error :

FAILURE: Build failed with an exception.  * What went wrong: 
Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

This error occurs because  of many factors.

Case 1 :  When your app and the libraries it references exceeds 64K limit

There is a 64K limit for adding dependencies, beyond which you will need to add multidex support as enabled to properly compile dependencies.

In order to fix the issue go to your app level build.gradle and make the following change

defaultConfig {
   ...
   ...
   multiDexEnabled true
}

Making this change will help you solve the issue in above lollipop devices(greater than 5.0). But for devices < 5.0, this may produce unexpected results or NoClassDefFoundError


So for devices < 5.0 , do the following
  1. Add this dependency in app level build.gradle >   compile 'com.android.support:multidex:1.0.0'
  2. You need to change the application in the manifest to the multidex application, or override attachBaseContext() in your custom application class to call MutiDex.install(this) after the super.attachBaseContext(base);  see more info here

Case 2 : Duplicate entries of same dependencies

Also, suppose you are adding a lot of dependencies to your project. Now each of these dependencies may internally use other dependencies as well. In this way, there is a possibility of same dependencies to get added up more than once.

In order to check which dependencies are being internally used by other dependencies you could print the gradle dependencies tree like shown below.



  • Select View > Tool Windows > Gradle (or click Gradle icon in the tool windows bar).


  • Expand AppName > Tasks > android and double-click androidDependencies. After Gradle executes the task, the Run window should open to display the output.

  • The output tree structure will be something like this

    +--- MyApp:mylibrary:unspecified
    |    \--- com.android.support:appcompat-v7:25.3.1
    |         +--- com.android.support:animated-vector-drawable:25.3.1
    |         |    \--- com.android.support:support-vector-drawable:25.3.1
    |         |         \--- com.android.support:support-v4:25.3.1
    |         |              \--- LOCAL: internal_impl-25.3.1.jar
    |         +--- com.android.support:support-v4:25.3.1
    |         |    \--- LOCAL: internal_impl-25.3.1.jar
    |         \--- com.android.support:support-vector-drawable:25.3.1
    |              \--- com.android.support:support-v4:25.3.1
    |                   \--- LOCAL: internal_impl-25.3.1.jar
    

    From this structure we can easily identify which all dependencies are being internally used by other dependencies

    Suppose you have a facebook sdk dependency which use support-v4 internally. But we already have a different version of support-v4 already added in dependencies list. When we try to compile the project, we may see that gradle sync has failed due to finished with non-zero exit value 2. The fix for this issue will be to exclude the internally used dependency from facebook sdk like shown below

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

    By specifying like this, it will no longer compile the internal dependency, but will only compile the dependency which we have already defined in the dependencies list

    No comments:

    Post a Comment