Android Method Counts: Overcome Dalvik 65K methods limit
Hello to all MultiDexers 🙂
It happens in the blink of an eye. Before, you are an happy Android developer, head down on your (or your company’s) application, adding the coolest libraries to provide more functionalities and to write simpler code. Afterwards, you stare at the dreaded output that states:
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
And you are stuck, unable to create the DEX file for the APK. You have no idea of its meaning, nor the slightest clue about how to get around it. All you can do is going all in for the most logic option: panic.
Getting to know your enemy
After you try all the usual tricks (which involve, in order: cleaning the project, restarting the IDE, building via command line, restarting your laptop and your friend’s too, and going out to see if in 10 minutes the problem disappears on its own), you face the hard truth: the problem is here to stay. So it might as well be worth knowing what is causing it.
Basically, it seems like you’ve hit something which is commonly (and improperly) referred to as the Dalvik 65K methods limit. In short (Thank you fadden):
You can reference a very large number of methods in a DEX file, but you can only invoke the first 65536, because that’s all the room you have in the method invocation instruction.
[…] the limitation is on the number of methods referenced, not the number of methods defined. If your DEX file has only a few methods, but together they call 70,000 different externally-defined methods, you’re going to exceed the limit.
The best tool I’ve found so far was made by Mihai Parparita, who coded a simple bash script called dex-method-counts. It is extremely fast and offers, in addition to the overall methods count, an XML-ish view of the packages together with their number of methods. Other solutions, such as dex.sh by the almighty Jake Wharton, gives the exact same result, but takes much more time due to its recursive approach (Jake also wrote an extremely interesting article on this very subject, you should read it as well).
Is there any other way?
There sure is. Running ProGuard on your application can help, since it strips the unnecessary methods from your code and, in addition, shrinks the size of your final APK. But don’t expect huge reductions, because there won’t be.
Another solution consists in creating a secondary DEX file, which contains parts of your code that you will access via interfaces/Reflection and which you will load via a custom ClassLoader (more info). This approach can prove to be more effective in certain circumstances (e.g. when you can easily isolate a module of your application, or when you only need a few methods from an external library).
We have multiple news on the DEX method limit problem! First of all, Google has officially acknowledged the problem and has provided us with the Multidex option. For a full coverage on this, read the official page. For an example, read this post by Alex Lipov.
Moreover, Google realized (oh my god, finally) that Google Play Services has become way too big. So, starting from the upcoming 6.5 version, we will have modular Play Services!
As we’ve continued to add more APIs across the wide range of Google services, it can be hard to maintain a lean app, particularly if you’re only using a portion of the available APIs. Now with Google Play services 6.5, you’ll be able to depend only on a minimal common library and the exact APIs your app needs. This makes it very lightweight to get started with Google Play services.
Read the full announcement and rejoice, Internet!
The Awesome link I found methodscount.com which is created by Sebastiano Gottardo to help fellow Android developers keeping track of how exactly how many methods does an Android library have. There’s also an Android Studio plugin which displays the info right on your build.gradle file! Be sure to check it out.
Source and Useful Links:
- [DEX] Sky’s the limit? No, 65K methods is
- Play Services 5.0 Is A Monolith Abomination
I did choose this article to copy and write here with some of my inputs, it is just because of some of the links are we cant able to find with great articles and just we missed it. So Its a help to share and get solution.
Feedbacks and opinions are extremely welcome. If you think I’ve made a mistake (probably more than one) or that I haven’t been clear enough on some subjects, please feel free to comment, I will happily correct it!
Hope you enjoyed the reading. ☺ 🙂 🙂 🙂
Page Views (185)