如何使用`--multi-dex`选项?

时间:2013-11-13 13:37:54

标签: android dalvik dx

[2013-11-13 18:39:09 - XlApp] Dx 
trouble writing output: Too many method references: 66024; max is 65536.
You may try using --multi-dex option.
References by package:
    13 java.lang
     1 java.lang.reflect
     5 java.util
     1 javax.xml.namespace
    66 org.apache.xmlbeans
    19 org.apache.xmlbeans.impl.values
     1 org.apache.xmlbeans.impl.xb.xmlschema
  2500 org.openxmlformats.schemas.drawingml.x2006.chart
  1430 org.openxmlformats.schemas.drawingml.x2006.chart.impl
  8767 org.openxmlformats.schemas.drawingml.x2006.main
  5258 org.openxmlformats.schemas.drawingml.x2006.main.impl
    86 org.openxmlformats.schemas.drawingml.x2006.picture
    33 org.openxmlformats.schemas.drawingml.x2006.picture.impl
   745 org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing
   417 org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.impl
   230 org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing
   164 org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.impl
   298 org.openxmlformats.schemas.officeDocument.x2006.customProperties
   256 org.openxmlformats.schemas.officeDocument.x2006.customProperties.impl
   617 org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes
   596 org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.impl
   285 org.openxmlformats.schemas.officeDocument.x2006.extendedProperties
   196 org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.impl
    23 org.openxmlformats.schemas.officeDocument.x2006.math
    24 org.openxmlformats.schemas.officeDocument.x2006.relationships
     2 org.openxmlformats.schemas.officeDocument.x2006.relationships.impl
  2076 org.openxmlformats.schemas.presentationml.x2006.main
  1224 org.openxmlformats.schemas.presentationml.x2006.main.impl
     1 org.openxmlformats.schemas.schemaLibrary.x2006.main
  7271 org.openxmlformats.schemas.spreadsheetml.x2006.main
  4556 org.openxmlformats.schemas.spreadsheetml.x2006.main.impl
 11448 org.openxmlformats.schemas.wordprocessingml.x2006.main
  9217 org.openxmlformats.schemas.wordprocessingml.x2006.main.impl
     4 schemaorg_apache_xmlbeans.system.sE130CAA0A01A7CDE5A2B4FEB8B311707
  1170 schemasMicrosoftComOfficeExcel
  1223 schemasMicrosoftComOfficeExcel.impl
   285 schemasMicrosoftComOfficeOffice
   124 schemasMicrosoftComOfficeOffice.impl
     2 schemasMicrosoftComOfficePowerpoint
     3 schemasMicrosoftComOfficeWord
  2858 schemasMicrosoftComVml
  2529 schemasMicrosoftComVml.impl
[2013-11-13 18:39:09 - XlApp] Conversion to Dalvik format failed with error 2

当我在我的android项目中包含5个外部.jar文件时,我收到此错误。我不知道该怎么做。请帮帮我!!!

我想到了做这个建议     您可以尝试使用--multi-dex选项

但我找不到在任何地方使用此选项的方法。

6 个答案:

答案 0 :(得分:7)

如果您使用的是Gradle / Android Studio,则可以将其添加到您的gradle配置中:

android {
....
   dexOptions {
    preDexLibraries = false
   }
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = []
        }
        dx.additionalParameters += '--multi-dex'

        // this is optional
        // dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
    }
}

然后,您需要添加位于sdk/extras/android/support/multidex/library/libs的multidex支持库jar。并通过从MultiDexApplication扩展您的应用程序或从您的应用程序的Multidex.install()方法调用attachBaseContext来安装它。

有关详细信息,请查看此博文:http://blog.osom.info/2014/10/multi-dex-to-rescue-from-infamous-65536.html

更新:

在这里https://developer.android.com/tools/building/multidex.html,您可以找到将Gradex用于Gradle的官方方式。

基本上你需要改变你的gradle文件:

android {
   compileSdkVersion 21
   buildToolsVersion "21.1.0"

   defaultConfig {
       ...
       minSdkVersion 14
       targetSdkVersion 21
       ...

       // Enabling multidex support.
       multiDexEnabled true
   }
   ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

将Application类设置为android.support.multidex.MultiDexApplication,或者如果已有Application类,则可以覆盖attachBaseContext()方法并调用MultiDex.install(this)以启用multidex。

答案 1 :(得分:3)

更新:您现在可以在以下页面找到有关如何配置multi dex的详细说明:https://developer.android.com/tools/building/multidex.html

简单的答案是,目前Android Studio工具不允许您指定生成多个dex文件的选项(--multi-dex)。因此,您需要手动编写构建过程脚本......真正的痛苦。

有几个看似相关的Android错误:6393620814

答案 2 :(得分:1)

来自javadoc文档:

usage:
  dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]
    [--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [--no-strict]
    [--keep-classes] [--output=<file>] [--dump-to=<file>] [--dump-width=<n>]
    [--dump-method=<name>[*]] [--verbose-dump] [--no-files] [--core-library]
    [--num-threads=<n>] [--incremental] [--force-jumbo]
    [--multi-dex [--main-dex-list=<file> [--minimal-main-dex]]
    [<file>.class | <file>.{zip,jar,apk} | <directory>] ...
     Convert a set of classfiles into a dex file, optionally embedded in a
     jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a directory.
     Positions options: none, important, lines.\n
    --multi-dex: allows to generate several dex files if needed. This option is
      exclusive with --incremental, causes --num-threads to be ignored and only
      supports folder or archive output.
    --main-dex-list=<file>: <file> is a list of class file names, classes defined by
      those class files are put in classes.dex.
    --minimal-main-dex: only classes selected by --main-dex-list are to be put in
      the main dex.

dx是一个用于dexing作为android构建过程的一部分的工具。在用于默认android构建的build.xml(位于sdk.dir/tools/ant/buildxml)中,它用作target name="-dex"的一部分,在宏dex-helper中声明。它已经过时Anttask仍然不支持--multi-dex。

dexer源代码中, - main-dex-list选项接收文件输入,其中包含您想要在主dex中的所有类文件,用新行分隔。像这样:

bin/classes/com/example/MainActivity.class

此列表中未声明的所有其他类将转到辅助dex。

或者您可以在之前使用旧方式--multi-dex介绍:http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

答案 3 :(得分:0)

现在,我编辑

.../sdk/build-tools/19.0.3/dx

替换最后一行:

exec java $javaOpts -jar "$jarpath" --multi-dex "$@"

这是一个黑客。这是不可维护的。可能你的CI管理员会恨你......

但它正在运作!

答案 4 :(得分:0)

如您所知,如果使用ant build,您必须稍微修改一下位于工具中的build.xml。 您将输出属性从存档更改为目录(即$ project / bin),并且dx步骤将成功完成。 可能是这样,但是ant-tasks.jar和sdklib.jar需要修改才能完成“apkbuilder”#39;步骤

  • ApkBuilder,ApkBuilderTask类

答案 5 :(得分:0)

如果有人在这段时间后绊倒了,您可以查看要添加的库。通常这个错误:添加大量库时会出现trouble writing output: Too many method references: 66024; max is 65536.。我看过很多次的一个例子就是当你想要添加像google-play-analytics这样的特定谷歌播放服务时,你会添加整个google-play库。尝试添加特定库。这将解决您的问题。