Updated 24 January 2024
The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.
But it is risky to follow because it may cause your application to run slow or may crash. There is a story behind it:
The installation of .dex files during startup onto a device’s data partition is complex and can result in Application Not Responding (ANR) errors if the secondary dex files are large. In this case, you should apply code shrinking techniques with ProGuard to minimize the size of dex files and remove unused portions of code.
Applications that use multidex may not start on devices that run versions of the platform earlier than Android 4.0 (API level 14) due to a Dalvik linearAlloc bug (Issue 22586). If you are targeting API levels earlier than 14, make sure to perform testing with these versions of the platform as your application can have issues at startup or when particular groups of classes are loaded. Code shrinking can reduce or possibly eliminate these potential issues.
When your application has multiple declarations of library in Gradle or has increased the size of the library and jar files than the given limits by android architecture, build system reports this error as follows:
1 2 3 4 5 6 7 8 9 10 11 |
com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:502) at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:277) at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:491) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:168) at com.android.dx.merge.DexMerger.merge(DexMerger.java:189) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:302) at com.android.dx.command.dexer.Main.run(Main.java:245) at com.android.dx.command.dexer.Main.main(Main.java:214) at com.android.dx.command.Main.main(Main.java:106) |
1) Remove some jar file from Libs folder and copy to some other folder, And
Go to Project Properties > Select Java Build Path, Select Libraries, Select Add External Jar, Select the Removed jar to your project, Click save.this will be added under Referenced Library instead of Libs folder. Now clean and Run your project.
this will be added under Referenced Library instead of Libs folder. Now clean and Run your project.
2) It would be better to work with Proguard. It reduces the unnecessary code from our application. It will decrease the size of the dex file by half.
3) Setting up your app development project to use a multidex configuration.
In particular, you need to perform the following steps to use multidex:
Change your Gradle build configuration to enable multidex
Modify your manifest to reference the MultiDexApplication class
Modify your app Gradle build file configuration to include the support library and enable multidex output, as shown in the following Gradle build file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
android { compileSdkVersion 23 buildToolsVersion "23.1.0" defaultConfig { ... minSdkVersion 15 targetSdkVersion 22 ... // Enabling multidex support. multiDexEnabled true } ... } dependencies { compile 'com.android.support:multidex:1.0.0' } |
In your manifest add the MultiDexApplication class from the multidex support library to the application element.
1 2 3 4 5 6 7 8 9 |
<?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> |
When using instrumentation tests with multidex apps, additional configuration is required to enable the test instrumentation. Because the location of code for classes in multidex apps is not within a single DEX file, instrumentation tests do not run properly unless configured for multidex.
To test a multidex app with instrumentation tests, configure the MultiDexTestRunner from the multidex testing support library. The following sample build.gradle file demonstrates how to configure your build to use this test runner:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
android { defaultConfig { ... testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner" } } You may use the instrumentation test runner class directly or extend it to fit your testing needs. Alternatively, you can override onCreate in existing instrumentations like this: public void onCreate(Bundle arguments) { MultiDex.install(getTargetContext()); super.onCreate(arguments); ... } |
Reference: http://developer.android.com/tools/building/multidex.html
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.