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

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

Are you stuck with above two lines while building your android app? Yes this happens when you are building large application which has added many references libraries in your code.
Before moving forward to check solutions for this issue, few things which one should know about why this issue occurs.

Why this issue occurs :

As you go on building large applications, your code size reaches certain limit for total number of methods in the app. Total number of methods referenced by the app does not only include methods written by you but also include methods bundled in external jars, library files which you have added into your project. You must be knowing architecture of android app (APK) which internally contains bytecode files in the form on Dalvik executable (DEX) files. Android has put an upper limit on total number of methods referenced by a single DEX file and which is 65536.
Now you can see 65536 in error which you are getting which itself signifies DEX file of your APK has referenced more than 65536 methods. And this is why issue - Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 occurs in your code.

Avoiding reaching 65536 method limit

Taking few cautious decisions and careful thinking while writing your code since beginning can save you. Always check you are not keeping any unused code in code files. Whenever you are adding any third party lib, you use most of the library functions instead of adding your own. Make sure you do not add any large lib just for couple of methods.
Reusable, neat and clean code and proper architecture of project can help you to avoid reaching 65536 methods limit.

How to resolve?

Android architecture has also provided solution to resolve this issue. Multidex files is the one solution which you can use to resolve issue of 65536 methods limit. But there are other simple approaches which one can use to resolve this issue. I will discuss multidex file support sometime later in another post. Let’s see initial step towards resolving issue i.e ProGuard

ProGuard Configuration :

Android has provided ProGuard tool which optimizes your code by removing unused code, methods. This reduces size of your app (APK). By default ProGuard configuration is not enabled when you create Android project. One can configure ProGuard by following few steps.

  1. ProGuard tool is found in your SDK path at {SDK_FOLDER}/tools/proguard/
  2. Above mentioned ProGuard folder from SDK has proguard-android.txt which plays major role when you enable ProGuard setting for your project
  3. Open project.properties file in edit mode (any simple editor will suffice) which will be in your project folder
  4. You can following see line in it

This line must be preceded with # (# is to comment whole line in project.properties file). Un comment this line by removing preceded #. Your SDK path has to be set in environment variables.

  1. Your configuration is done. Clean the code, build it and run it. If it runs successfully you must see proguard.txt file in bin folder of your project.
  2. After configuring ProGuard, if app still shows same issue of 65536 limit you can try un checking Private Libraries from build path in project properties. Please see below screen shot for reference.

    After this step again clean the code, build and run the app

ProGuard is not full proofed solution to resolve 65K methods limit. However ProGuard will resolve the issue if DEX file from reduced size APK contains methods less than 65K after shrinking, obfuscation of code and removal of unusedcode code by ProGuard tool. If final DEX file still references more than 65536 method then one may have to go for multi DEX solution!