DexOverflowException: Cannot fit requested classes in the main-dex file

眉间皱痕 提交于 2019-12-08 02:54:02

问题


I have quite big multi module, multi flavor multidex project, but recently I got error when trying to do command line build (assemble{flavor}Release)

Message is:

DexOverflowException: Cannot fit requested classes in the main-dex file

Here is more logs from --debug build:

/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)

How can I control what goes to main dex file? How to force main dex file to contain less methods?

My setup:

compileSdkVersion 27
buildToolsVersion '27.0.3'

gradlew:

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

回答1:


I tried Alexander Bratusenko answer, didn't help but its a nice clue. What actually helped - I downgraded gradle plugin to

classpath 'com.android.tools.build:gradle:3.1.4'



回答2:


For release build you should try to fix that with proguard. For debug you can try to fix it by excluding some classes from main dex file.

Add this to the application module

   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 
                ]))
            }
        }

you can find classes that gradle wants to add to main dex in: intermeditates/legacy_multidex_main_dex_list/. For example you can exclude unused models or something else, basically everything you don't need when Application class is starting. Do not forget to check that you didn't broke anything on API 19 or lower

set up buildSrc with build.gradle with groovy plugin.

At buildSrc/main/groovy/your/awesome/app add your action:

@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
    }
}

Too see the resulting method count you can use apkAnalyzer tool. Look for first dex.

Also you can try to refactor your code and extract some responsibilities from application class.

You can also try to increase minApi to 21.

This answer has some more details on the problem Too many classes in --main-dex-list, main dex capacity exceeded




回答3:


Disabling keeping runtime annoted classes may help. Just put it in android{} section of every module gradle file

dexOptions {
        preDexLibraries = false
        keepRuntimeAnnotatedClasses false
    }

Usually this happens when in project heavely used dagger2




回答4:


What actually happening here is you are crossing the dex method count 65536 (which means you can use only 65536 methods in your apk) and you are just 12 methods ahead 😎

DexOverflowException: Cannot fit requested classes in the main-dex file (# methods: 65548 > 65536)

Best suggestion is you should really decrease the no. of methods count.

  • Remove the unused library
  • If you are using only a part of the library try to define your own method and implement it.

This link will be helpful to observe your dex method count.

But if you are really in need of using the dex method count more than 65536 you should check out this Enable multidex

happy coding!



来源:https://stackoverflow.com/questions/51950860/dexoverflowexception-cannot-fit-requested-classes-in-the-main-dex-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!