8

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.

Wesley
  • 4,084
  • 6
  • 37
  • 60

1 Answers1

12

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.

Alex Lipov
  • 13,503
  • 5
  • 64
  • 87
  • I get the following errors when attempting this: UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.RuntimeException: --minimal-main-dex: file not found UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.RuntimeException: --set-max-idx-number=50000: file not found UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.RuntimeException: --output: file not found Error:Execution failed for task ':app:dexGoogleProdDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\dvltools\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1 – BenDroid Jun 08 '15 at 14:53
  • Looks like I just had to take the spaces out: command << '--minimal-main-dex' command << '--set-max-idx-number=50000' – BenDroid Jun 08 '15 at 15:27
  • @AlexLipov , can you share the build.gradle source, link seems to be broken – Manish Jul 09 '15 at 07:02
  • @Manish Tested again, both links work well. I don't have a full build.gradle file online, just embed the snippet into your build script. Let me know if you have any problems.. – Alex Lipov Jul 09 '15 at 07:07
  • 3
    It seems Amazon added this to their official documentation: https://developer.amazon.com/public/support/submitting-your-app/tech-docs/submitting-an-app-that-references-65000-methods – joecks Jul 28 '15 at 13:10
  • Updated Amazon docs link: https://developer.amazon.com/docs/app-submission/avoid-compile-errors.html – TalkLittle Apr 16 '18 at 21:27