33

I'm using the new gradle build system and I'm facing the following problem:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/kibo/mobi/BuildConfig;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

Priniting the dependencies I can't see anything, here they are:

 firstDebugCompile - ## Internal use, do not manually configure ##
 \--- KiboGradle:KiboSDK:unspecified
 +--- KiboGradle:TextInputAPI:unspecified
 +--- KiboGradle:VoiceImeUtils:unspecified
 +--- com.google.android.gms:play-services:5.0.77
 |    \--- com.android.support:support-v4:19.1.0
 +--- com.squareup.picasso:picasso:2.3.2
 +--- com.google.code.gson:gson:2.2.4
 \--- com.crittercism:crittercism-android-agent:4.5.1

I tried to verify that the problem is not a duplicate support library so I tried to add:

compile ('com.google.android.gms:play-services:5.0.77'){
    exclude module: 'support-v4'
}

Which resulted in errors that some of the support-v4 library classes can't be found, so this library not getting compiled from any other location.

One thing I had in mind that could cause this problem is the fact that I using the Flavors feautre in oreder to create several versions of my application with different resourse files.

And when I look at the file that is in the error I see this:

**
* Automatically generated file. DO NOT MODIFY
*/
package com.kibo.mobi;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String PACKAGE_NAME = "com.kibo.mobi.test.official";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "liverpool";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

So the package in of the file and the package specified in String value are not the same.

Can anyone see any issues in my configuration that could cause this problem?

1
  • This happens to me too. Interestingly buids from command line are successful, only when running from Studio it fails
    – Gavriel
    Commented Jan 15, 2018 at 12:54

6 Answers 6

89

In my case the similar error happened because there were 2 modules with the same package name in AndroidManifest.xml files. Using different package names in the modules solved the problem.

Also the same thing happens when a library jar is being included twice (or more times) in several modules, as a dependency. In this case error message says about duplicate configs named after that library's package name. I solved it with including the library as a dependency in one module, and the second module had in dependencies the first module.

3
  • By the way, I have to delete the build directory of the library. Clean not delete it... Commented Jan 8, 2018 at 13:59
  • I had this problem while I was building an android library. I had two modules (one library module and one sample application module) both having same package name. I was able to resolve the problem by renaming the package name for one of the module. Commented Feb 15, 2018 at 1:53
  • 5
    Took me 1.5 days to discover this.
    – Elye
    Commented May 26, 2018 at 5:51
24

Add this to your build.gradle:

android {
    dexOptions {
        preDexLibraries = false
    }
}

I suppose this way there is no conflicting BuildConfig.java.

EDIT:

Why the above works: Android studio will first dex the libraries before dex-ing the app module. If you have a library module with the same package name as your app module, this 'pre-dexing' will result in the creation of a BuildConfig.java in the same package as for the app.

Note: 'pre-dexing' will slow down your build process a bit so I suggest that you change your library's package name instead.

5
  • 1
    it works for me. I am not sure there gonna be a consequence to use this approach or not, but it works. Thank a ton !!!!
    – Thai Tran
    Commented Nov 30, 2017 at 12:55
  • Thank! It save my life )))))) Simple changing library package name.
    – maXp
    Commented May 24, 2018 at 23:23
  • Unfortunately this didn't work for me. And if I change the base module name then com.google.gms.google-services fail. Damn it! Commented Aug 30, 2018 at 14:19
  • Thanks a lot ! This answer worked for me, you saved my day, my life.
    – Dnyanesh M
    Commented Apr 13, 2020 at 7:22
  • 'dexOptions(Action<DexOptions>): Unit' is deprecated. Setting dexOptions is obsolete.
    – YektaDev
    Commented Jul 22, 2021 at 4:29
11

For me, simply doing a clean on the project cleared this error.

8

I was getting this problem signing my instant apk. The problem:

bad module/app/manifest.xml

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

good: module/app/manifest.xml

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

Just adding the .app at the end of the package name

2

in my case there were two namespaces that are the same. You cane follow these steps

  1. click shift shift for searching
  2. search for the package name in your case com.kibo.mobi, so search for mobi
  3. you can find two places or may be more in different places check the wrong places for its build.gradle in android block check namespace for duplications

like

android {
    namespace 'com.xyz.duplicatedPackage'
}

change to

android {
    namespace 'com.xyz.yourRightPackageName'
}
1
  • 1
    This was my problem too Thanks!
    – Isaac Lee
    Commented Oct 9, 2022 at 0:56
0

If using NewRelic, then update it to at least 5.21.1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.