How to Solve Dex 65K Method Exception

While you gradually develop your application, you most probably would be encountered by the famous unable to execute dex exception.

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

or

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

You get this error mainly because there is a limitation on dalvik executable files and apparently the limit is 65536 which is 2^16 (two to the power sixteen).

This problem is also known as 65k method limit. So this limit is applied on the method count of your application. Android sdk methods are also counted. Therefore it is not that hard to reach out that limit. If you add a few third-party libraries to your gradle file and also android support libraries and google play services library, eventually you will hit the limit.

How to solve Unable to execute dex exception?

You have to enable multi-dex configuration for your android application. Multi-dex is a configuration which lets your application to use more than one dex executable file.

How to enable multi-dex configuration?

 

To enable multi-dex configuration you how to apply the following steps:

First step is to edit your build.gradle file of your main module and add the highlighted lines into it.

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.1'

    defaultConfig {
        applicationId "com.gunhansancar.example.appid"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
}
    buildTypes {
        release {
            minifyEnabled false
        }
    }

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

Second step is to tell your application class to handle these changes. To do that you have two options.

A – You can edit your AndroidManifest.xml file to apply your changes to the application tag.

<?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>

Most commonly you have already defined your own application class here. Therefore, you have to go the B option.

B – You can edit your custom application class to enable multi-dex support.

public class MainApplication extends Application {
    
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

Limitations

Apparently there are limitation below API Level 14 and below. Your app may not compile or run below API level 14. While compiling you can get OutOfMemory exception.

There are some configuration that can help you to increase heap size so that with you can compile huge applications.

In your gradle file you can add the following settings:

    dexOptions {
        jumboMode true
        javaMaxHeapSize "4g"
    }

 

If you encounter any other problems, feel free to ask.

Related posts


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *