Type BuildConfig is defined multiple times
I'm working on a multi module Android application and everything was working in a debug mode but now when I try to build a release package I'm receiving this error:
Type ***module1.BuildConfig is defined multiple times:
***/module1/build/intermediates/runtime_library_classes/release/classes.jar:***/module1/BuildConfig.class,
***/module2/build/intermediates/runtime_library_classes/release/classes.jar:***/module1/BuildConfig.class
It's the first time I'm seeing an error like this and I don't know how to fix this and what's even causing it. As far as I'm aware library modules shouldn't even be generating BuildConfig files in release mode.
check if both modules have a same package name
My Android Manifest Had the same Package Name for two Diferent Modules.
Most probably both shared AndroidManifest.xml and androidApp AndroidManifest.xml has the same packageId. They have to be different. For example, com.my.app.shared and com.my.app.android
I'm using Android Studio 4.0.1 on MacOS
On my network drive within the project app
folder, I searched for BuildConfig
I noticed a BuildConfig 2.class
file had been generated.
I deleted the BuildConfig 2.class
file, rebuilt, re-ran and everything was fine.
UPDATE
I ran into a situation where a BuildConfig 3.class
showed up, so now I search by Config
(Config+space)
UPDATE 2
I have this issue continuously every time I run. Now rather than deleting the file, I use, menu item
"Build"->"Clean Project"
Then re-run the app.
Rebuilding the app fixed the issue for me.
If rebuilding is not fixing the issue Check this out : https://developer.android.com/studio/build/dependencies#duplicate_classes