无法执行dex:方法ID不在[0,0xffff]中:65536

本文翻译自:Unable to execute dex: method ID not in [0, 0xffff]: 65536

I have seen various versions of the dex erros before, but this one is new. 我以前见过各种版本的dex erros,但这是新的。 clean/restart etc won't help. 清理/重新启动等无济于事。 Library projects seems intact and dependency seems to be linked correctly. 图书馆项目似乎完好无缺,并且依存关系似乎已正确链接。

Unable to execute dex: method ID not in [0, 0xffff]: 65536
Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536

or 要么

Cannot merge new index 65950 into a non-jumbo instruction

or 要么

java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

tl;dr : Official solution from Google is finally here! tl; dr :来自Google的官方解决方案终于来了!

http://developer.android.com/tools/building/multidex.html http://developer.android.com/tools/building/multidex.html

Only one small tip, you will likely need to do this to prevent out of memory when doing dex-ing. 只有一个小技巧,您可能需要执行此操作,以防止在进行排序时内存不足。

dexOptions {
        javaMaxHeapSize "4g"
}

There's also a jumbo mode that can fix this in a less reliable way: 还有一种巨型模式可以用不太可靠的方式解决此问题:

dexOptions {
        jumboMode true
}

Update: If your app is fat and you have too many methods inside your main app, you may need to re-org your app as per 更新:如果您的应用很胖,并且您的主应用中包含太多方法,则可能需要按照

http://blog.osom.info/2014/12/too-many-methods-in-main-dex.html http://blog.osom.info/2014/12/too-many-methods-in-main-dex.html


#1楼

参考:https://stackoom.com/question/11olr/无法执行dex-方法ID不在-xffff-中


#2楼

Your project is too large. 您的项目太大。 You have too many methods. 您有太多方法。 There can only be 65536 methods per application. 每个应用程序只能有65536个方法。 see here https://code.google.com/p/android/issues/detail?id=7147#c6 看到这里https://code.google.com/p/android/issues/detail?id=7147#c6


#3楼

I've shared a sample project which solve this problem using custom_rules.xml build script and a few lines of code. 我共享了一个示例项目,该示例项目使用custom_rules.xml构建脚本和几行代码来解决此问题。

I used it on my own project and it is runs flawless on 1M+ devices (from android-8 to the latest android-19). 我在自己的项目中使用了它,并且它在1M +设备(从android-8到最新的android-19)上完美运行。 Hope it helps. 希望能帮助到你。

https://github.com/mmin18/Dex65536 https://github.com/mmin18/Dex65536


#4楼

The perfect solution for this would be to work with Proguard. 完美的解决方案是与Proguard合作。 as aleb mentioned in the comment. 正如评论中提到的aleb。 It will decrease the size of the dex file by half. 它将使dex文件的大小减少一半。


#5楼

The below code helps, if you use Gradle. 如果您使用Gradle,则以下代码会有所帮助。 Allows you to easily remove unneeded Google services (presuming you're using them) to get back below the 65k threshold. 允许您轻松删除不需要的Google服务(假设您正在使用它们),使其恢复到65k阈值以下。 All credit to this post: https://gist.github.com/dmarcato/d7c91b94214acd936e42 此帖子的全部功劳: https : //gist.github.com/dmarcato/d7c91b94214acd936e42

Edit 2014-10-22 : There's been a lot of interesting discussion on the gist referenced above. 编辑2014-10-22 :关于上面提到的要点,有很多有趣的讨论。 TLDR? TLDR? look at this one: https://gist.github.com/Takhion/10a37046b9e6d259bb31 看看这个: https : //gist.github.com/Takhion/10a37046b9e6d259bb31

Paste this code at the bottom of your build.gradle file and adjust the list of google services you do not need: 将此代码粘贴到build.gradle文件的底部,并调整不需要的Google服务列表:

def toCamelCase(String string) {
    String result = ""
    string.findAll("[^\\W]+") { String word ->
        result += word.capitalize()
    }
    return result
}

afterEvaluate { project ->
    Configuration runtimeConfiguration = project.configurations.getByName('compile')
    ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
    // Forces resolve of configuration
    ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion

    String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
    File playServiceRootFolder = project.tasks.find { it.name.equals(prepareTaskName) }.explodedDir

    Task stripPlayServices = project.tasks.create(name: 'stripPlayServices', group: "Strip") {
        inputs.files new File(playServiceRootFolder, "classes.jar")
        outputs.dir playServiceRootFolder
        description 'Strip useless packages from Google Play Services library to avoid reaching dex limit'

        doLast {
            copy {
                from(file(new File(playServiceRootFolder, "classes.jar")))
                into(file(playServiceRootFolder))
                rename { fileName ->
                    fileName = "classes_orig.jar"
                }
            }
            tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
                destinationDir = playServiceRootFolder
                archiveName = "classes.jar"
                from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
                    exclude "com/google/ads/**"
                    exclude "com/google/android/gms/analytics/**"
                    exclude "com/google/android/gms/games/**"
                    exclude "com/google/android/gms/plus/**"
                    exclude "com/google/android/gms/drive/**"
                    exclude "com/google/android/gms/ads/**"
                }
            }.execute()
            delete file(new File(playServiceRootFolder, "classes_orig.jar"))
        }
    }

    project.tasks.findAll { it.name.startsWith('prepare') && it.name.endsWith('Dependencies') }.each { Task task ->
        task.dependsOn stripPlayServices
    }
}

#6楼

Update 3 (11/3/2014) 更新3(11/3/2014)
Google finally released official description . 谷歌终于发布了官方描述


Update 2 (10/31/2014) 更新2(10/31/2014)
Gradle plugin v0.14.0 for Android adds support for multi-dex. 适用于Android的Gradle插件v0.14.0 添加了对多dex的支持 To enable, you just have to declare it in build.gradle : 要启用,只需在build.gradle中声明它即可

android {
   defaultConfig {
      ...
      multiDexEnabled  true
   }
}

If your application supports Android prior to 5.0 (that is, if your minSdkVersion is 20 or below) you also have to dynamically patch the application ClassLoader , so it will be able to load classes from secondary dexes. 如果您的应用程序支持5.0之前的Android版本(也就是说,如果您的minSdkVersion为20或更低),那么您还必须动态修补应用程序ClassLoader ,以便能够从二级dexes加载类。 Fortunately, there's a library that does that for you. 幸运的是,有一个可以为您完成此任务。 Add it to your app's dependencies: 将其添加到应用程序的依赖项中:

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

You need to call the ClassLoader patch code as soon as possible. 您需要尽快调用ClassLoader补丁代码。 MultiDexApplication class's documentation suggests three ways to do that (pick one of them , one that's most convenient for you): MultiDexApplication类的文档提出了三种实现方法(选择其中一种 ,一种对您来说最方便):

1 - Declare MultiDexApplication class as the application in your AndroidManifest.xml : 1-在您的AndroidManifest.xml MultiDexApplication类声明为应用程序:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

2 - Have your Application class extend MultiDexApplication class: 2-让您的Application类扩展MultiDexApplication类:

public class MyApplication extends MultiDexApplication { .. }

3 - Call MultiDex#install from your Application#attachBaseContext method: 3-从您的Application#attachBaseContext方法调用MultiDex#install

public class MyApplication {
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
        ....
    }
    ....
}

Update 1 (10/17/2014): 更新1(10/17/2014):
As anticipated, multidex support is shipped in revision 21 of Android Support Library. 如预期的那样 ,Android支持库的修订版21中提供了对multidex的支持 You can find the android-support-multidex.jar in /sdk/extras/android/support/multidex/library/libs folder. 您可以在/ sdk / extras / android / support / multidex / library / libs文件夹中找到android-support-multidex.jar。


Multi-dex support solves this problem. Multi-dex支持解决了这个问题。 dx 1.8 already allows generating several dex files. dx 1.8已经允许生成多个dex文件。
Android L will support multi-dex natively, and next revision of support library is going to cover older releases back to API 4. Android L将原生支持多dex,并且支持库的下一个版本将涵盖API 4之前的较早版本。

It was stated in this Android Developers Backstage podcast episode by Anwar Ghuloum. Anwar Ghuloum在 Android Developers Backstage播客集中对此进行了说明。 I've posted a transcript (and general multi-dex explanation) of the relevant part. 我已经发布了相关部分的成绩单 (和一般的多dex解释)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值