Android app build error: D8: XXXXXXX is defined multiple times when switching branches

9.1k views Asked by At

We are facing a peculiar issue while building our Android app. Everything compiles successfully when we develop on one branch. However, as soon as we switch to another branch (any branch other than the current one), we encounter the following error:

    D8: XXXXXXX is defined multiple times:
Type com.xxxxxxxxx is defined multiple times: /Users/xxxxx/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/75dc6161aec3efe974a08e13687ced704791b9c6e7797bbab5932f7bab205fc9_14.jar:classes.dex, /Users/xxxxx/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/ff31007b66c63e303d30cc9551267d87131906985d359c93f6793be7006cde58_14.jar:classes.dex

 com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives.

Please note that the XXXXXXX part changes every time, and it refers to a different class each time.

We would appreciate any insights or suggestions on how to permanently resolve this problem. Thank you!

To resolve this issue temporarily, we have to clean the project, after which it builds successfully until the next branch switch.

Because the duplicated class changed all the time. So I tried to find if there is any same package name in different modules, nothing found.

--- UPDATE --- The full build output is too long too display (over 30k lines), here is the error part:


> Task :L5_cell:createNormal_DebugApkListingFileRedirect
> Task :L5_cell:assembleNormal_Debug
> Task :L5_shortvideo:transformNormal_DebugClassesWithAsm
> Task :L5_tag_module:transformNormal_DebugClassesWithAsm
> Task :L5_biz_724:transformNormal_DebugClassesWithAsm

> Task :L5_cell:processSplitApkNormal_Debug
ApkResign> app> sign apk:/Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/L5_cell-master-unsigned.apk
mZipAlignShell:/Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/linux/build_tools/27.0.3/zipalign
mSignJar:/Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/apksigner_27.0.3.jar
KeyStore文件OK~ 大小(KeyStore is OK~, file size):1361
Start re-sign
: mBaseApkPath:/Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/L5_cell-master-unsigned.apk
: mSignJar Path:/Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/apksigner_27.0.3.jar
: mZipAlignJar Path:/Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/linux/build_tools/27.0.3/zipalign
: mShouldDeleteKeyStore:false
filePath:/META-INF/MANIFEST.MF
delete success filePath:/META-INF/MANIFEST.MF
filePath:/META-INF/ANDROID_.RSA
delete success filePath:/META-INF/ANDROID_.RSA
filePath:/META-INF/ANDROID_.SF
delete success filePath:/META-INF/ANDROID_.SF
mZipAlignCmd: [/Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/linux/build_tools/27.0.3/zipalign, -v, 4, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/unsigned_temp.apk, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/L5_cell-master-unsigned.apk]
==After zip align==
[java, -jar, /Users/username/Document/Tcode/AppNameLiveEmbed/sign/MakeApkTools/apksigner_27.0.3.jar, sign, --ks, /tmp/20191210_temp_file, --ks-pass, pass:[email protected], --in, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/L5_cell-master-unsigned.apk, --out, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/qigsaw/split-outputs/apks/normal_Debug/tmp/L5_cell/L5_cell-master-unsigned.apk]
==Re-sign success==
Qigsaw Build Message: Inject activity com.xxxxxxxxxx.xxxs.kkvideo.shortvideo.VerticalVideoVideoActivity

> Task :L5_shortvideo:transformClassesWithSplitResourcesLoaderForNormal_Debug
SplitComponentTransform cost 11761 ms

> Task :L5_biz_724:transformClassesWithSplitResourcesLoaderForNormal_Debug
SplitComponentTransform cost 16545 ms

> Task :app:mergeProjectDexNormal_Debug FAILED
ERROR:/Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar: D8: Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
    at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:151)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:138)
    at com.android.build.gradle.internal.tasks.DexMergingWorkAction.merge(DexMergingTask.kt:859)
    at com.android.build.gradle.internal.tasks.DexMergingWorkAction.run(DexMergingTask.kt:805)
    at com.android.build.gradle.internal.profile.ProfileAwareWorkAction.execute(ProfileAwareWorkAction.kt:74)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:66)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:62)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:62)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:59)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:205)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:187)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.access$700(DefaultConditionalExecutionQueue.java:120)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner$1.run(DefaultConditionalExecutionQueue.java:162)
    at org.gradle.internal.Factories$1.create(Factories.java:31)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:270)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:119)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:124)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:157)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:126)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex
    at Version.fakeStackEntry(Version_3.0.78.2.java:0)
    at com.android.tools.r8.utils.ExceptionUtils.failWithFakeEntry(ExceptionUtils.java:139)
    at com.android.tools.r8.utils.ExceptionUtils.failCompilation(ExceptionUtils.java:89)
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:83)
    at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:64)
    at com.android.tools.r8.D8.run(D8.java:124)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:136)
    ... 38 more
Caused by: com.android.tools.r8.utils.AbortException: Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
    at com.android.tools.r8.utils.Reporter.handleDiagnostic(Reporter.java:81)
    at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:127)
    at com.android.tools.r8.utils.ProgramClassCollection.reportDuplicateTypes(ProgramClassCollection.java:74)
    at com.android.tools.r8.utils.ProgramClassCollection.lambda$defaultConflictResolver$1(ProgramClassCollection.java:68)
    at com.android.tools.r8.utils.ProgramClassCollection.lambda$create$0(ProgramClassCollection.java:28)
    at java.base/java.util.concurrent.ConcurrentHashMap.merge(ConcurrentHashMap.java:2048)
    at com.android.tools.r8.utils.ProgramClassCollection.create(ProgramClassCollection.java:27)
    at com.android.tools.r8.graph.LazyLoadedDexApplication$Builder.build(LazyLoadedDexApplication.java:239)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:177)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:132)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:102)
    at com.android.tools.r8.D8.readApp(D8.java:178)
    at com.android.tools.r8.D8.run(D8.java:193)
    at com.android.tools.r8.D8.lambda$run$1(D8.java:127)
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:80)
    ... 41 more

Qigsaw Build Message: Inject activity com.xxxxxxxxxx.xxxs.tag.page.CategoryActivity

> Task :L5_tag_module:transformClassesWithSplitResourcesLoaderForNormal_Debug
SplitComponentTransform cost 18560 ms

> Task :L5_shortvideo:transformClassesWithSplitLibraryLoaderForNormal_Debug


> Task :L5_biz_724:transformClassesWithSplitLibraryLoaderForNormal_Debug


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeProjectDexNormal_Debug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
   > There was a failure while executing work items
      > A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingWorkAction
         > com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
           Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
           Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:mergeProjectDexNormal_Debug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:147)
    at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:282)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:145)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:133)
    at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:77)
    at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
    at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
    at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:74)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:333)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:320)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:313)
    at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:299)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.lambda$run$0(DefaultPlanExecutor.java:143)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:227)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:218)
    at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:140)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingTaskDelegate
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:207)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:187)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.access$700(DefaultConditionalExecutionQueue.java:120)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner$1.run(DefaultConditionalExecutionQueue.java:162)
    at org.gradle.internal.Factories$1.create(Factories.java:31)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:270)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:119)
    at org.gradle.internal.work.DefaultWorkerLeaseService.runAsWorkerThread(DefaultWorkerLeaseService.java:124)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:157)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:126)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
Caused by: org.gradle.workers.WorkerExecutionException: There was a failure while executing work items
    at org.gradle.workers.internal.DefaultWorkerExecutor.workerExecutionException(DefaultWorkerExecutor.java:267)
    at org.gradle.workers.internal.DefaultWorkerExecutor.await(DefaultWorkerExecutor.java:249)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:72)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker$1.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
    at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
    at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.execute(NoIsolationWorkerFactory.java:59)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork$2(DefaultWorkerExecutor.java:205)
    ... 11 more
Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing com.android.build.gradle.internal.tasks.DexMergingWorkAction
    at org.gradle.workers.internal.DefaultWorkerExecutor$WorkItemExecution.waitForCompletion(DefaultWorkerExecutor.java:339)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.lambda$waitForItemsAndGatherFailures$2(DefaultAsyncWorkTracker.java:131)
    at org.gradle.internal.Factories$1.create(Factories.java:31)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withoutLocks(DefaultWorkerLeaseService.java:341)
    at org.gradle.internal.work.DefaultWorkerLeaseService.withoutLocks(DefaultWorkerLeaseService.java:326)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:127)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForItemsAndGatherFailures(DefaultAsyncWorkTracker.java:89)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForAll(DefaultAsyncWorkTracker.java:79)
    at org.gradle.internal.work.DefaultAsyncWorkTracker.waitForCompletion(DefaultAsyncWorkTracker.java:67)
    at org.gradle.workers.internal.DefaultWorkerExecutor.await(DefaultWorkerExecutor.java:247)
    ... 25 more
Caused by: com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
    at com.android.build.gradle.internal.tasks.DexMergingWorkAction.merge(DexMergingTask.kt:872)
    at com.android.build.gradle.internal.tasks.DexMergingWorkAction.run(DexMergingTask.kt:805)
    at com.android.build.gradle.internal.profile.ProfileAwareWorkAction.execute(ProfileAwareWorkAction.kt:74)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:66)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1$1.create(NoIsolationWorkerFactory.java:62)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:97)
    at org.gradle.workers.internal.NoIsolationWorkerFactory$1.lambda$execute$0(NoIsolationWorkerFactory.java:62)
    ... 24 more
Caused by: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
    at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:151)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:138)
    at com.android.build.gradle.internal.tasks.DexMergingWorkAction.merge(DexMergingTask.kt:859)
    ... 31 more
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex
    at Version.fakeStackEntry(Version_3.0.78.2.java:0)
    at com.android.tools.r8.utils.ExceptionUtils.failWithFakeEntry(ExceptionUtils.java:139)
    at com.android.tools.r8.utils.ExceptionUtils.failCompilation(ExceptionUtils.java:89)
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:83)
    at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:64)
    at com.android.tools.r8.D8.run(D8.java:124)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:136)
    ... 32 more
Caused by: com.android.tools.r8.utils.AbortException: Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times: /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex, /Users/username/Document/Tcode/AppNameLiveEmbed/app/build/intermediates/mixed_scope_dex_archive/normal_Debug/out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex
    at com.android.tools.r8.utils.Reporter.handleDiagnostic(Reporter.java:81)
    at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:127)
    at com.android.tools.r8.utils.ProgramClassCollection.reportDuplicateTypes(ProgramClassCollection.java:74)
    at com.android.tools.r8.utils.ProgramClassCollection.lambda$defaultConflictResolver$1(ProgramClassCollection.java:68)
    at com.android.tools.r8.utils.ProgramClassCollection.lambda$create$0(ProgramClassCollection.java:28)
    at com.android.tools.r8.utils.ProgramClassCollection.create(ProgramClassCollection.java:27)
    at com.android.tools.r8.graph.LazyLoadedDexApplication$Builder.build(LazyLoadedDexApplication.java:239)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:177)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:132)
    at com.android.tools.r8.dex.ApplicationReader.read(ApplicationReader.java:102)
    at com.android.tools.r8.D8.readApp(D8.java:178)
    at com.android.tools.r8.D8.run(D8.java:193)
    at com.android.tools.r8.D8.lambda$run$1(D8.java:127)
    at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:80)
    ... 35 more


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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 14m 51s
10752 actionable tasks: 1909 executed, 615 from cache, 8228 up-to-date
Build scan background action failed.
2

There are 2 answers

1
Krokomot On

Gradle optimizes the building procedure inter alia in two ways -- incremental builds and build caching.

Daily Life

Lets start with no surprises. To cite Gradle's docs:

An incremental build is a build that avoids running tasks whose inputs did not change since the previous build, making the execution of such tasks unnecessary. For incremental builds to work, tasks must define their inputs and outputs. At build time, Gradle will determine whether the input or outputs have changed. If they have changed, Gradle will execute the task. Otherwise, it will skip execution.

Incremental Builds [...] help avoid work that is already done. If a developer is continuously making changes in a single file, there is likely no need to rebuild all the other files in the project.

The Gordian Knot

So far, so clear. One could think that switching the branch will seamlessly continue the incremental build procedure on that new branch. However:

What happens when the same developer decides to switch to a new branch created last week? The files are rebuilt, even though the developer is building something that has been built before.

It's a new branch and from Gradle's perspective nothing has been done here yet, though there are still the old result files from the old branch. That's why you get lines like (reformatted):

ERROR:
/.../build/intermediates/.../out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:

D8: Type com.xxxxxxxxxx.ams.dynamicwidget.BuildConfig is defined multiple times:
/.../build/intermediates/.../out/1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar:classes.dex
/.../build/intermediates/.../out/375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar:classes.dex

The newly created

1703be4408d6a5fd21e9e69c71e1314b194fe435af52fc6e7692eeeb63105993_13.jar

conflicts with the intermediate file

375fc00ebbbf2a453f2f11eb16313b871cac6c4588166b726e8a4a3afec805fa_13.jar

from the old branch, leading D8 to see a multiple definition of the same.

Alexander's Sword

The solution is to use Gradle's Build Cache mechanism. The few steps needed to employ it are in detail described in the corresponding documentation on how to enable the build cache, so I just referenced them for self-reading. Instead I want to direct your attention on some points:

  1. Incremental Building is per default turned on.
  2. But Build Caching is per default turned off.
  3. The Cache will serve all projects and project-branches.

Gradle will look at the cache directory on your machine to check for output files that may already exist. If they do, instead of running that task, it will copy its (output) results into your project build directory.

  1. From time to time the local build cache will be cleaned by removing files that have not been used recently.
0
AnumShafiq On

In my opinion, your code has different lib versions on different branches which is causing this error with different classes that are using that particular library.