-1

I am new To android studio. I have made a Java project which mainly works with an SQLite db (in assets folder). I migrated This project to android studio from netbeans. When I press run and choose an emulator, Gradle Build Tasks start to execute and an error occurs which says:

    Error:Execution failed for task 
    ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
    > java.lang.RuntimeException: 
    com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

The Complete error output in console says:

    * What went wrong:
   Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
   > java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

   * Try:
   Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

   * Get more help at https://help.gradle.org

   BUILD FAILED in 39s

This is the Stack trace after pressing 'run'

Executing tasks: [:app:assembleDebug]

    :app:preBuild UP-TO-DATE
    :app:preDebugBuild UP-TO-DATE
    :app:compileDebugAidl UP-TO-DATE
    :app:compileDebugRenderscript UP-TO-DATE
    :app:checkDebugManifest UP-TO-DATE
    :app:generateDebugBuildConfig UP-TO-DATE
    :app:prepareLintJar UP-TO-DATE
    :app:generateDebugResValues UP-TO-DATE
    :app:generateDebugResources UP-TO-DATE
    :app:mergeDebugResources UP-TO-DATE
    :app:createDebugCompatibleScreenManifests UP-TO-DATE
    :app:processDebugManifest
    :app:splitsDiscoveryTaskDebug UP-TO-DATE
    :app:processDebugResources
    :app:generateDebugSources
    :app:javaPreCompileDebug UP-TO-DATE
    :app:compileDebugJavaWithJavac UP-TO-DATE
    :app:compileDebugNdk NO-SOURCE
    :app:compileDebugSources UP-TO-DATE
    :app:mergeDebugShaders UP-TO-DATE
    :app:compileDebugShaders UP-TO-DATE
    :app:generateDebugAssets UP-TO-DATE
    :app:mergeDebugAssets UP-TO-DATE
    :app:transformClassesWithDexBuilderForDebug UP-TO-DATE
    :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED 

There are a lot of solution on the web like putmultiDexEnabled true in gradle.build file which I already had.

Update This is my gradle.build file

    apply plugin: 'com.android.application'

   android {
       compileSdkVersion 26
       defaultConfig {
           applicationId "com.example.bcosta.myapplication"
           minSdkVersion 23
           targetSdkVersion 23
           versionCode 1
           versionName "1.0"
           testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
           multiDexEnabled = true
       }
       buildTypes {
           release {
               minifyEnabled false
               proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
           }
       }
   }

   dependencies {
       implementation fileTree(include: ['*.jar'], dir: 'libs')

       implementation 'com.android.support:appcompat-v7:26.1.0'
       implementation 'com.android.support.constraint:constraint layout:1.0.2'
       testImplementation 'junit:junit:4.12'
       androidTestImplementation 'com.android.support.test:runner:1.0.1'
       androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
          }
bcsta
  • 1,963
  • 3
  • 22
  • 61

2 Answers2

0

Try this,

  android {
    defaultConfig {
        ...
        multiDexEnabled true
    }
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

If you don’t have a custom Application class, then add this line to manifest

<application
   android:name="android.support.multidex.MultiDexApplication"
   .....your code

If you already have your own Application class, make it extend android.support.multidex.MultiDexApplication instead of android.app.Application

import android.support.multidex.MultiDexApplication;


public class MyApplication extends MultiDexApplication {

    /* To enable Multi-Dex, you have 3 options -
        1. Declare android.support.multidex.MultiDexApplication as your application class in AndroidManifest.xml
        2. Extend MultiDexApplication like we are doing here
        3. Override attachBaseContext() in your application class as shown below
    /*
     */
    /*
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    */
}
Pramod Waghmare
  • 1,273
  • 13
  • 21
  • I did not change anything from the build.gradle for multidex. the 'multiDexEnabled true' was there by default – bcsta Jan 12 '18 at 11:05
  • I have 2 build.gradle files one for the app module and the other for the project. For which one should I add this? ( my multiDexEnabled true is in the module build file) – bcsta Jan 12 '18 at 11:11
  • build.gradle(Module:app) – Pramod Waghmare Jan 12 '18 at 12:01
  • @user7331538 In your gradle, there is no value for `multiDexEnabled`. It should be `multiDexEnabled = true` – riadrifai Jan 12 '18 at 12:29
  • Tried first suggestion since I dont have myapplicatin class. I have a very normal java project where I am running its main class from mainActivity. Tried your updated answer still with no improvement. – bcsta Jan 12 '18 at 14:51
0

This seems like a common problem with no definitive solution.

Try:

  1. Delete the .gradle directory.
  2. Clean project and build again.

If that doesn't work, try adding the following to your build.gradle:

android {
dexOptions{
   jumboMode=true;
   javaMaxHeapSize "4g"
}
}

Also, try File -> Invalidate Caches/Restart

riadrifai
  • 1,108
  • 2
  • 13
  • 26