0

Whenever I am Trying to Re-Build or Run my Code, following Error is coming: Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

I have checked many solutions but none solved it. Following is the link. Unable to Merge Dex - Android Studio 3.0

Running gradle console with stack tree gave 4 errors:

  1. Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

  2. Error:java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

  3. Error:com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

  4. Error:com.android.dex.DexException: Multiple dex files define Landroid/arch/lifecycle/LiveData;

Running with debug-option gave :

Error:10:49:47.799 [ERROR] [system.err] Note: Some input files use or override a deprecated API. 10:49:47.799 [ERROR] [system.err] Note: Recompile with -Xlint:deprecation for details. 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception. 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong: 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try: 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run with --stacktrace option to get the stack trace. 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Get more help at https://help.gradle.org 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] 10:50:01.875 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 1m 7s

Please, Help I am making a Chat App using Firebase. I am stuck here for 2 days, I am unable to continue further.

My (build.gradle) file.

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.imalok.saapchat"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
dexOptions {
    preDexLibraries = false
    javaMaxHeapSize "4g"
}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.0'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:multidex:1.0.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-auth:11.0.4'
    implementation 'com.android.support:support-v4:27.1.0'
    implementation 'de.hdodenhof:circleimageview:2.2.0'
    implementation 'com.google.firebase:firebase-database:11.0.4'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
    implementation 'com.firebaseui:firebase-ui-database:2.3.0'
    implementation 'id.zelory:compressor:2.0.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.google.firebase:firebase-storage:11.0.4'

    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

apply plugin: 'com.google.gms.google-services'

build.gradle

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:3.1.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imalok.saapchat">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application

    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".StartActivity" />
    <activity
        android:name=".RegisterActivity"
        android:parentActivityName=".StartActivity" />
    <activity
        android:name=".LoginActivity"
        android:parentActivityName=".StartActivity" />
    <activity android:name=".SettingsActivity" />
    <activity
        android:name=".StatusActivity"
        android:parentActivityName=".SettingsActivity" />
    <activity
        android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
        android:theme="@style/Base.Theme.AppCompat" />
    <activity
        android:name=".UsersActivity"
        android:parentActivityName=".MainActivity" />
    <activity android:name=".ProfileActivity"></activity>
</application>

Edit:1 Will you guys remove the duplicate level. Mine issue was different and so was the solution, as I posted.

Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
Alok Raj
  • 55
  • 1
  • 12

4 Answers4

4

The transform dex merger issue was persistent.. Until I added a dependency :: implementation 'android.arch.lifecycle:extensions:1.1.0'

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Alok Raj
  • 55
  • 1
  • 12
  • This solved the problem for me when nothing else would. These DEX errors are very strange. My project was working perfectly on one machine, moved it to another and was hit with this error. Everything about the set-up is identical. – Steven Elliott Mar 06 '18 at 22:16
  • Great, nothing else worked for me except for this! – zen_1991 Mar 20 '18 at 07:20
1

you have enabled multidex by :

multiDexEnabled true

but you are not using multidex library.
Add multidex library to your dependency :

implementation 'com.android.support:multidex:1.0.3'

and In your Application class :

public class YouApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}

For more refere this question :

How to enable multidexing with the new Android Multidex support library

Milan Hirpara
  • 534
  • 4
  • 18
  • But I am using min sdk version 21, so do I need to add multiDexEnabled? – Alok Raj Mar 01 '18 at 04:36
  • Multidexenabling is not necessary but it helps apps to install faster and it splits our code into multiple dex files, you can use any min sdk version , it is not worthy. if you don't wants to use multidex then just delete 'multiDexEnabled true' line from your defaultConfig {} section in build.gradle – Milan Hirpara Mar 01 '18 at 04:37
  • //Api lower than 14 doesn't support multidex – Milan Hirpara Mar 01 '18 at 04:40
  • I tried deleting that and re-building the project but the error remained same. by the way Can You guide me How can I add in application class. Actually I am new to AS, so I have no idea about Application class – Alok Raj Mar 01 '18 at 04:43
0

You can use dexOptions for this.

android{
   dexOptions {
       javaMaxHeapSize "4g"
   }
}

Place this in build.gradle file of app. Place dexOptions in android block of build.gradle file

Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21
0

Add below dependency in your app gradle file because you mentioned multiDexEnabled true in your gradle.

implementation 'com.android.support:multidex:1.0.3'

after that

extend your application class by MultiDexApplication

and then keep you old code as it is in your application class

and also mention below following terms in your app gradle file.

android { 
dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }
}