How to solve the issue with Dalvik compiler limitation on 64K methods?

My team and I have inherited a large Android project from another team. The whole application with all the included libraries is reported to have around 35000 methods. We now have the task to implement a new service in the app where we need to use Protocol Buffers.

The problem is that the generated .jar file with all the required .proto files creates another couple of 35000 methods, that’s 70000 methods. And if you are not aware, the Android compiler has a limitation of 65536 methods per .dex file. We are clearly over that limit and we are getting the following error trying to compile the app:

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

Yes, the application architecture should probably be restructured but that will take time. And for now we are trying to figure out a solution to work around this problem temporarily.

Any suggestions?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can use another DEX file. This is how you do it:

http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html

Solution 2

Enable Proguard (http://developer.android.com/tools/help/proguard.html) to remove unused methods. The protobuf generator creates thousands of methods that are never actually used.

Micro-protobuffers (https://code.google.com/p/micro-protobuf/) may also be useful.

Solution 3

Square had similar issues and they built Wire to deal with the method explosion caused by protobufs. They claim to have killed 10,000 methods.

Solution 4

In versions of Google Play services prior to 6.5, you had to compile the entire package of APIs into your app. In some cases, doing so made it more difficult to keep the number of methods in your app (including framework APIs, library methods, and your own code) under the 65,536 limit.

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

compile 'com.google.android.gms:play-services:6.5.87'

with these lines:

compile 'com.google.android.gms:play-services-fitness:6.5.87'
compile 'com.google.android.gms:play-services-wearable:6.5.87'

for more reference, you can click here

Solution 5

If this is the first use of Protocol buffers, you could look at alternative JavaME
implementations i.e.

there are others listed in Third party add ons. If have not used any of them, but they seem to be smaller and do not have all the methods created by the standard protocol buffers.

Solution 6

We’ve recently added Nano Protobufs to Android which significantly reduces the number of methods generated.

Solution 7

If you are using eclipse this is the easiest work around Click Here!

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply