在 Android Studio 中使用 MultiDex 功能时,有没有办法限制主 dex 文件中的方法数量 [英] Is there a way to limit method amount in main dex file while using MultiDex feature in Android Studio

查看:28
本文介绍了在 Android Studio 中使用 MultiDex 功能时,有没有办法限制主 dex 文件中的方法数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我像文档所说的那样在 Android Studio 中启用 MultiDex 功能时,它自动分成两个或多个 dex 文件.我无法配置它.而且似乎在主 dex 文件中,方法的数量非常接近限制(65536).

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

问题是如何配置它,使主dex文件中的方法数量减少到一定数量,比如60k.我得把apk上传到amazon appstore,amazon的人会在主dex文件中添加一些方法,使其超过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.

推荐答案

dx 工具有以下选项:

dx tool has the following options:

  • --minimal-main-dex - 将仅将 main-dex-list 选择的类放入主 dex.
  • --set-max-idx-number - 未公开的选项,用于确定每个 dex 文件的最大方法数.
  • --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.

您必须通过指定这两个选项来自定义您的 build.gradle 脚本.例如(来源):

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

请注意,如果您的主 dex 太大,这将无济于事.有一些规则控制哪些类应该放在主索引中.我在这里写了关于它们的博客.

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.

编辑 (1-9-2016):
Android 插件 1.5 版不允许向 dx 添加额外参数,但 似乎它将在即将推出的版本之一中修复.

这篇关于在 Android Studio 中使用 MultiDex 功能时,有没有办法限制主 dex 文件中的方法数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆