产品风格,重复 class,合并 dex 档案时出错

Product Flavor, Duplicate class, Error while merging dex archives

我在添加 Gradle-AspectJ 库(即 apply plugin: 'com.archinamon.aspectj' )后才收到此错误。
没有flavors,在新项目中没有这个问题,一切正常。
如果您遇到过这个问题并且有答案,请告诉我。我在这上面花了太多时间,尝试了我能想到的一切并找到了。

根级别build.gradle:

buildscript { 
    ext.kotlin_version = "1.3.72"
    ext.detekt_version = "1.9.1"

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        ...
        classpath "com.archinamon:android-gradle-aspectj:4.2.1"
    }
}

...

allprojects {
    repositories {
        google()
        jcenter()
...
        flatDir {
            dirs 'lib'
        }
    }
}
...

app/build.gradle:


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
...
apply plugin: 'com.archinamon.aspectj'
...

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId ""
        minSdkVersion 24
        targetSdkVersion 29
...
    }

    aspectj {
        includeAspectsFromJar "SecLib3.3.0"
        includeAllJars false
        ajcArgs << "-Xlint:ignore"
        excludeJar "mpulse", "aspectj"
        weaveInfo true
        compileTests = false
    }

    ...

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    flavorDimensions "server", "datasource"

    productFlavors {
        mock {
            // Mocked network layer
            dimension "datasource"
        }
        live {
            // Real network layer, different URLs based on "server" flavor dimension
            dimension "datasource"
        }

        fat {
            dimension "server"
            ...
        }
        ...
    }

    /* Removing unused flavor dimension combinations:
        - demo and prod mockDebug
     */
    variantFilter { variant ->
        if (variant.getFlavors().get(0).name != 'fat'
                && variant.getFlavors().get(1).name == 'mock') {
            variant.setIgnore(true)
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
...

    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Kotlin KTX
    implementation "androidx.core:core-ktx:1.3.0"

    // androidx
    implementation "androidx.appcompat:appcompat:1.1.0"
    implementation "androidx.constraintlayout:constraintlayout:2.0.0-beta7"
    implementation "androidx.cardview:cardview:1.0.0"

    // Material Components
    implementation "com.google.android.material:material:1.2.0-beta01"
...
}

我遇到的错误:

AGPBI: {"kind":"error","text":"Type android.support.v4.app.INotificationSideChannel is defined multiple times: /home/...../app/build/intermediates/project_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex, ...../app/build/intermediates/mixed_scope_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex","sources":[{"file":"...../app/build/intermediates/project_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex"}],"tool":"D8"}
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 android.support.v4.app.INotificationSideChannel is defined multiple times: ...../app/build/intermediates/project_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex, ...../app/build/intermediates/mixed_scope_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex
    at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:132)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:119)
    at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:102)
    at com.android.build.gradle.internal.tasks.DexMergingTaskRunnable.run(DexMergingTask.kt:441)
    at com.android.build.gradle.internal.tasks.Workers$ActionFacade.run(Workers.kt:242)
    at org.gradle.workers.internal.AdapterWorkAction.execute(AdapterWorkAction.java:50)
    at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:50)
    at org.gradle.workers.internal.NoIsolationWorkerFactory.create(NoIsolationWorkerFactory.java:63)
    at org.gradle.workers.internal.NoIsolationWorkerFactory.create(NoIsolationWorkerFactory.java:59)
    at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:98)
    at org.gradle.workers.internal.NoIsolationWorkerFactory.lambda$execute[=13=](NoIsolationWorkerFactory.java:59)
    at org.gradle.workers.internal.AbstractWorker.call(AbstractWorker.java:44)
    at org.gradle.workers.internal.AbstractWorker.call(AbstractWorker.java:41)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:165)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
    at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
    at org.gradle.workers.internal.AbstractWorker.executeWrappedInBuildOperation(AbstractWorker.java:41)
    at org.gradle.workers.internal.NoIsolationWorkerFactory.execute(NoIsolationWorkerFactory.java:53)
    at org.gradle.workers.internal.DefaultWorkerExecutor.lambda$submitWork(DefaultWorkerExecutor.java:200)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runExecution(DefaultConditionalExecutionQueue.java:215)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.runBatch(DefaultConditionalExecutionQueue.java:164)
    at org.gradle.internal.work.DefaultConditionalExecutionQueue$ExecutionRunner.run(DefaultConditionalExecutionQueue.java:131)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:48)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
    at com.android.tools.r8.utils.W.a(:87)
    at com.android.tools.r8.D8.run(:11)
    at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:117)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete

我设法解决了这个问题。

问题的主要原因是依赖项(firebase-corefirebase-crashlyticsfirebase-perf)中的重复。也许 aspectj 与他们有关?

删除这三个 Firebase 依赖项后,问题似乎消失了。

/app/build/intermediates/mixed_scope_dex_archive/fatMockDebug/out/android/support/v4/app/INotificationSideChannel.dex

只需删除包含文件的文件夹(app) INotificationSideChannel.dex app文件夹在上面的路径中用粗体字母高亮显示 运行 android 工作室中的项目。现在这个错误不来了。