Andrew Dex exceeds the number of pit method

Introduction
Before doing the project, our manager told me that some users can not install the application, enter the application after installation on a black screen or just flash back. I was shocked out of his face forced, after a variety of tossing finally found the error log:

UNEXPECTED TOP-LEVEL EXCEPTION:  
java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536  
at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:501)  
at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:282)  
at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:490)  
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:167)  
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)  
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)  
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)  
at com.android.dx.command.dexer.Main.run(Main.java:230)  
at com.android.dx.command.dexer.Main.main(Main.java:199)  
at com.android.dx.command.Main.main(Main.java:103)  

After a careful search of discover this is a Android itself "bug", that is, the number of ways to limit the application itself must not exceed 65,535. Android versions prior to 5.0, each android application contains only a dex file, depending on the application will result in flash back (Friends of the Union, such high moral map) because the project itself plus the various third-party sdk in your project the number of third-party library method has exceeded the upper limit of 65535. google subcontract mechanism multidex introduced to the problem, when generating the apk, the entire application dex split into n packets (classes.dex, classes2.dex, classes3.dex), each of no more than 64k dex methods. But the system prior to 5.0 this question you need to configure it manually.

Use
general we would usually write in doing the project a class that inherits Application, in the class of the class to initialize some value, or a single column, so long as the class file member rewrite attachBaseContext method folder, so that it loads the application starts second, third ... a dex file, this problem can be circumvented.

public class MyApplication extends Application {
    public static MyApplication instance;

    public static MyApplication getInstance() {
        return instance;
    }

    public void onCreate() {
        super.onCreate();
        //单列返回值(能让程序在任意地方取到context)
        instance = this;
        init();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }
}    

Or directly inherited MultiDexApplication this class.

public class MyApplication extends MultiDexApplication{
  // ...........
}

OK, start running the project, the result has exploded.
This is because the default Dalvik class loader will look for classes.dex, so it is necessary to combine them in order to make the identification. That added gradle library files referenced in your project

 defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        dexOptions{
            preDexLibraries = false
        }
    }

You can run the project on a 4.0 system configuration is complete.

Released four original articles · won praise 4 · Views 1407

Guess you like

Origin blog.csdn.net/heromrwang/article/details/82623974