Home » Android » Is there a way to limit method amount in main dex file while using MultiDex feature in Android Studio

Is there a way to limit method amount in main dex file while using MultiDex feature in Android Studio

Posted by: admin June 15, 2020 Leave a comment

Questions:

When I enabled MultiDex feature in Android Studio like the document says, it automatically spilted into two or more dex files. I cannot config it. And it seems that in the main dex file, the amount of methods is very close to the limitation(65536).

The question is how to config it, make the amount of methods in the main dex file reduce to a certain number, say 60k. I have to upload the apk to amazon appstore, and the people of amazon will add a few methods into the main dex file and make it over the 65536 limit.

How to&Answers:

dx tool has the following options:

  • –minimal-main-dex – will put only classes that selected by main-dex-list into the main dex.
  • –set-max-idx-number – undocumented option that determines maximum number of methods per single dex file.

You have to customize your build.gradle script by specifying those two options. In example (source):

tasks.withType(com.android.build.gradle.tasks.Dex) {    dexTask ->
  def command = [] as List
  command << ' --minimal-main-dex'
  command << ' --set-max-idx-number=50000'
  dexTask.setAdditionalParameters(command)
}

Note that this won’t help if your main dex is too large. There’re rules that govern which classes should be placed in main dex. I blogged about them here.

Edit (1-9-2016):
Version 1.5 of Android plugin doesn’t allow to add additional parameters to dx, but it seems that it will be fixed in one of the upcoming versions.