当前位置: 首页 > 知识库问答 >
问题:

DexOverflowException:main-dex文件中不能容纳请求的类

韩照
2023-03-14
DexOverflowException: Cannot fit requested classes in the main-dex file
/Users/bartek/Documents/workspace/android/argus-android/Argus/app/build/intermediates/transforms/dexBuilder/argus/release/1114.jar
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:125)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:110)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:101)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:36)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   ... 47 more
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:76)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:45)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.D8.run(D8.java:67)
15:54:42.645 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:108)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   ... 49 more
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: com.android.tools.r8.utils.AbortException
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:77)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:67)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:62)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   ... 52 more
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]   Suppressed: com.android.tools.r8.errors.DexOverflowException: Cannot fit requested classes in the main-dex file (# methods: 65548 > 65536)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.dex.VirtualFile.throwIfFull(VirtualFile.java:182)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.dex.VirtualFile$DistributorBase.fillForMainDexList(VirtualFile.java:305)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.dex.VirtualFile$FillFilesDistributor.run(VirtualFile.java:353)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.dex.ApplicationWriter.distribute(ApplicationWriter.java:167)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.dex.ApplicationWriter.write(ApplicationWriter.java:187)
15:54:42.646 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.D8.run(D8.java:167)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.D8.lambda$run$0(D8.java:71)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:58)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:45)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.tools.r8.D8.run(D8.java:67)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:108)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:101)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:36)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ForkJoinTask.externalAwaitDone(ForkJoinTask.java:326)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ForkJoinTask.doJoin(ForkJoinTask.java:391)
15:54:42.647 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ForkJoinTask.join(ForkJoinTask.java:719)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:397)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.transforms.DexMergerTransform.transform(DexMergerTransform.java:221)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.lang.reflect.Method.invoke(Method.java:497)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
15:54:42.648 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:124)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:113)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:95)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:73)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:59)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:59)
15:54:42.649 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:101)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.FinalizeInputFilePropertiesTaskExecuter.execute(FinalizeInputFilePropertiesTaskExecuter.java:44)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:88)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:62)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
15:54:42.650 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:623)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:578)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
15:54:42.651 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]       at java.lang.Thread.run(Thread.java:745)

我的设置:

compileSdkVersion 27
buildToolsVersion '27.0.3'

格拉德卢:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip
plugin: classpath 'com.android.tools.build:gradle:3.2.0-alpha04'

共有1个答案

宗苗宣
2023-03-14

对于发布版本,您应该尝试用ProGuard来修复这个问题。对于调试,您可以尝试通过从主dex文件中排除一些类来修复它。

将此添加到应用程序模块

   import your.awesome.app.MainDexGenerator

   tasks.whenTaskAdded {
            if (it.name.contains('transformDexArchiveWithDexMergerFor') && it.name.contains('Debug')) {
                it.doFirst(new MainDexGenerator([
                        'io/reactivex/annotations',
                        'kotlin',
                        'com/fasterxml'
                        //....and other you think could work 
                ]))
            }
        }

您可以在:intermeditates/legacy_multidex_main_dex_list/中找到gradle想要添加到main dex的类。例如,您可以排除未使用的模型或其他东西,基本上排除应用程序类启动时不需要的所有东西。不要忘记检查您在API19或更低版本上没有损坏任何东西

@CompileStatic
class MainDexGenerator implements Action<Task> {

    private static final String MAIN_DEX_FILE = 'mainDexList.txt'

    // list we want to exclude frome MainDex
    private List<String> excludedList
    MainDexGenerator(List<String> excludedPackages) {
        excludedList = excludedPackages;
    }

    @Override
    void execute(Task task) {
        for (File inputFile : task.inputs.files.files) {
            if (inputFile.absolutePath.endsWith(MAIN_DEX_FILE)) {
                List<String> result = Files.lines(inputFile.toPath())
                        .filter { isNotMatch(it) }
                        .collect(Collectors.toList())
                Files.write(inputFile.toPath(), result, WRITE, TRUNCATE_EXISTING)
                break
            }
        }
    }

    private boolean isNotMatch(String line) {
        for (String item : excludedList) {
            if (line.contains(item)) {
                return false
            }
        }
        return true
    }
}

此外,您还可以尝试重构代码,并从应用程序类中提取一些职责。

您也可以尝试将minApi增加到21。

这个答案有更多关于问题的细节--main-dex-list中的类太多,main dex容量超过

 类似资料: