How to avoid DEX 64K LIMIT by importing the right library

后端 未结 5 782
栀梦
栀梦 2021-02-14 11:03

I had this issue where I overflow the 64k method limit in Dalvik when compiling all the library I used. I started to have this issue when I imported the Support

5条回答
  •  清酒与你
    2021-02-14 12:05

    Though using Multidex Support Library library solves the problem, it is not perfect. The first thing to do should be to determine how many methods your app currently has and how many of them are being added by each of its dependencies. Earlier you had to do this manually, but now there are a bunch of tools available:

    1. Use a Gradle plugin – This is an extremely easy to use Gradle plugin some time back that lists the number of methods by package along with the total number. You can find more information about it here.
    2. www.methodscount.com – Just enter the ‘compile’ statement on this website and it’ll tell you it’s method count, dependencies, JAR size and DEX size.
    3. Android Studio Plugin – This excellent plugin shows the method count of each dependency right inside Android Studio.

    Irrespective of what you tool you end up using, use the information it provides to perform an audit of your app’s dependencies. You should look for unused libraries or ones that can be minimized or even replaced with your own simpler solution. If you are not using Proguard to remove unused code, first enable it and see if it solves the problem(Check this). Using Proguard is well… not fun, but once you manage to make it work properly, it’ll significantly reduce the method count. If all else fails, you’ll have to use the MultiDex support library.

    Check out gradle task to strip unused packages on Google Play Services library.

    Conclusion

    While the Multidex Support Library fixes the DEX 64K problem in most cases, it should be treated as a last resort. Before attempting to use it, you should audit your project for unwanted dependencies and remove as much unused code as possible using ProGuard. If you do go ahead and use it, make sure you test your app on older devices.

    Hope this will help you.

提交回复
热议问题