Catherine
Catherine

Reputation: 14000

Multiple dex files define BuildConfig

I am migrating a project from Eclipse to Android Studio and have run into a problem. I have a lot of library modules which are shared between different projects, and several of these library modules have the same package name, so I end up with the following error:

AGPBI: {"kind":"SIMPLE","text":"com.android.dex.DexException: Multiple dex files define Lcom/foo/bar/BuildConfig;","position":{},"original":"com.android.dex.DexException: Multiple dex files define Lcom/foo/bar/BuildConfig;"}

My dependencies are fine, so that's not the problem:

compile - Classpath for compiling the main sources.
+--- project :project1
|    +--- com.mixpanel.android:mixpanel-android:4.6.0
|    \--- com.google.android.gms:play-services-gcm:7.5.0
|         \--- com.google.android.gms:play-services-base:7.5.0
|              \--- com.android.support:support-v4:22.0.0
|                   \--- com.android.support:support-annotations:22.0.0
+--- project :facebook
|    \--- com.parse.bolts:bolts-android:1.1.4
+--- com.google.android.gms:play-services-location:7.5.0
|    \--- com.google.android.gms:play-services-maps:7.5.0
+--- project :project2
|    \--- project :project3
|         \--- project :project4
|              \--- project :project5
+--- project :project6
\--- project :project7

However, when I search my directories for BuildConfig.java, I get the following results:

./project1/build/generated/source/buildConfig/androidTest/debug/com/foo/bar/test/BuildConfig.java
./project1/build/generated/source/buildConfig/debug/com/foo/bar/BuildConfig.java
./project1/build/generated/source/buildConfig/release/com/foo/bar/BuildConfig.java
./project5/build/generated/source/buildConfig/androidTest/debug/com/foo/bar/test/BuildConfig.java
./project5/build/generated/source/buildConfig/debug/com/foo/bar/BuildConfig.java
./project5/build/generated/source/buildConfig/release/com/foo/bar/BuildConfig.java
./project4/build/generated/source/buildConfig/androidTest/debug/com/foo/bar/test/BuildConfig.java
./project4/build/generated/source/buildConfig/debug/com/foo/bar/BuildConfig.java
./project4/build/generated/source/buildConfig/release/com/foo/bar/BuildConfig.java

It appears the problem is that I have several modules which share a namespace. Is there any way to get around that? I don't want to have to change the namespace of my other projects.

In a classic display of cargo cult programming, I tried adding:

dexOptions {
    preDexLibraries = false
}

to the android block in my app's build.gradle, but that only made it worse, and the build started complaining about other dependencies.

Are there any ways around this short of renaming the package names?

Upvotes: 4

Views: 10731

Answers (3)

Siddharth
Siddharth

Reputation: 9574

I had defined one library project. I included this library project as a Dependency on my main project. The BuildConfig was having the same package.

Library Project Manifest had

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.<companyname>.library.baseapp">

Main Project

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.<companyname>.library.baseapp">

The errors I got was

baseapp_android/build/.transforms/23204dbdbf5b7208d6261a7b35e24e97/classes/classes.dex
Type com.<companyname>.library.baseapp.BuildConfig is defined multiple times: /<path>/baseapptest_android/baseapp_android/build/.transforms/23204dbdbf5b7208d6261a7b35e24e97/classes/classes.dex, /<path>/baseapptest_android/baseapp_test/build/intermediates/project_dex_archive/debug/out/com/<companyname>/library/baseapp/BuildConfig.dex
com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/jsiddharth/AndroidStudioProjects/baseapptest_android/baseapp_android/build/.transforms/23204dbdbf5b7208d6261a7b35e24e97/classes/classes.dex
com.android.tools.r8.utils.b: Type com.<companyname>.library.baseapp.BuildConfig is defined multiple times: /<path>/baseapp_android/build/.transforms/23204dbdbf5b7208d6261a7b35e24e97/classes/classes.dex, /<path>/baseapptest_android/baseapp_test/build/intermediates/project_dex_archive/debug/out/com/<companyname>/library/baseapp/BuildConfig.dex
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 

I changed the Main Project manifest to

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.<companyname>.library.baseapp_test">

And it worked.

Upvotes: 2

Catherine
Catherine

Reputation: 14000

I solved this by changing the package in the manifest of of my sub-modules, which until then had had the same name for all my libraries.

Upvotes: 9

Asad Mehmood
Asad Mehmood

Reputation: 325

Main Problem is this line

Multiple dex files define Lcom/foo/bar/BuildConfig;

First try removing AppCompatLibraryfrom above location or from libs folder

This error occur because of when there are two same libraries exist in a single projects dependencies.

Upvotes: 0

Related Questions