Multiple dex files define <my package>/BuildConfig, can't find the cause: Multiple dex files define <my package>/BuildConfig, can't find the cause: android android

Multiple dex files define <my package>/BuildConfig, can't find the cause:


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.


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.


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