I recently in my project get 65536 error message about dex size and blahblah.
and now I'm trying to reduce my dex size but I don't know exactly wich of my libraries is adding more size to dex file I just can see hole dex file size in apk analyzer is there a tool or way to analyze dex file and getting information about how many methods this library has and how much adding to my dex file?
You should use this gradle plugin
https://github.com/KeepSafe/dexcount-gradle-plugin
It's the best to find the number of methods of your app in total or per library
Also if you prefer to check the methods count in advance, you can check this tool:
www.methodscount.com
UPDATE: Not maintained anymore
Just add multidex support in your project settings. That should solve your problem.
Related
there
I'm currently study Ajadx project and have found out there could be more than one classes.dex file.
Is there anyone who knows why and perhaps the evolving Android development history about it?
Thank you!
The maximum .dex (Dalvik Executable) operation (not dex size) for Java is 4G (means, 4 GigaBytes).
The "multidex enabled" means the system can create more than one dex files on need.
When happens dex operation?
It happens whenever you create apk (means, in apk build).
The maximum single dex size is 65K (means, 65 KiloBytes).
Note: 8K dex file can cross 65K dex operations if there are so many concurrent executions in your application. You could get the similar error even though you set total (max.) dex operation size is 4G. See this picture:
It is fine in build projects, but, you get this error when you try to build APK.
To solve this, you need to enable multidex. After you enable multidex and when you build, Android Studio generates corresponding first dex file from first 65K dex operation memory, 2nd dex file from second 65K dex operation memory and so on.
Now, when you change your build.gradle to enable multidex, you can build your APK.
Now, you apk file is ready. You uncompress your apk file and go inside that folder. You can see something like below picture. See this picture
You see, there are two dex files: classes.dex and classes2.dex. First classes.dex file is (in my example) is 8.3MB which is output of first 65K concurrent dex operations. And, the second classes2.dex file is output of rest dex operations which (We don't know exactly but we know it is less than 65K).
In this way, the overall dex operations for total dex files is 4G (4GB), which you asked here.
Hope, I gave your answer.
I referred the Android Documentation site. for "Multidex" but not getting idea when i use this. and it is mandatory to use this or not.For what purpose we need to use that class??
Thanks..
The purpose of this is to split up your Dex file into multiple Dex files.
The Dex file contains the compiled code of your application.
Android has a problem whereby there is an upper limit on the number of method definitions in a Dex file (64k). This means that once that limit is reached, you cannot expand your application any further.
Before multidex, it was advised to use ProGuard to reduce the number of method definitions by removing methods that aren't used in code. Especially useful when implementing the Google Play Services Framework.
Multidex was then introduced and allows Apps to continue to expand without worrying about method count limits. It does carry the danger of making the App more unstable. Therefore it is advised to try ProGuard first to reduce the method count.
according to documentation:
Android application (APK) files contain executable bytecode files in
the form of Dalvik Executable (DEX) files, which contain the compiled
code used to run your app. The Dalvik Executable specification limits
the total number of methods that can be referenced within a single DEX
file to 65,536, including Android framework methods, library methods,
and methods in your own code. Getting past this limit requires that
you configure your app build process to generate more than one DEX
file, known as a multidex configuration.
https://developer.android.com/studio/build/multidex.html
How do we count the number of methods in a Library file.
For eg, a Jar or AAR.
This is required to know the number of android resource IDs used by the library used by the project. This would help in estimating how close we are to 65K dex limit in Android.
dexcount-gradle-plugin: gradle plugin, works for any Android project (library or application), nice D3 sunburst partition chart output.
dex-method-counts: command-line tool, supports apk / zip / dex / directory.
Methods Count: only works for online public library, just give library 'compile' statement (e.g., com.google.code.gson:gson:2.4).
APK method count: only works for APK, expandable tree results.
Why don't you give this site a try, MethodsCount. It also gives you with an awesome Android Studio plugin which add method count beside all your libraries in the build.gradle file.
You can also try this and this. I have tried both of them and they are worth checking.
I've created a small shell script that uses a solution, provided on other StackOverflow Answer for jar files. Maybe still could be useful. It takes AAR file and returns methods count. But you have to have Android SDK installed for that.
I am implementing C2Call library for dial call purpose in one application, and C2Call library has lots of jar files, simple-xml-2.6.7.jar is part of C2Call library.
simple-xml-2.6.7.jar is giving me an error in my application at compile time:
[2014-08-21 11:09:56 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/simpleframework/xml/Attribute;
[2014-08-21 11:09:56 ] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lorg/simpleframework/xml/Attribute;
I've done a lot of surfing to solve this problem, but I can not find solution to resolve this problem.
I hope, you can give solution for this problem.
Thanks.
This problem happens if you are importing the same library in two different jars. You need to carefully check all the jars and try to find out the jars which are importing the same libraries. If you have the source code then you can easily build the jar again without adding the library.
Another thing to try is to break it into multiple dex files. Please read from here
http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html
"Big apps can contain more than 64K method references, which is the maximum number of supported in a dex file. To get around this limitation, developers can partition part of the program into multiple secondary dex files, and load them at runtime."
I think you have more than 64k method references and that is causing the problem. Let me know if it works for you
It happens when you try to export same jar file twice in single .apk file.
Simple way to eliminate this error
Check your:-
Android private libraries
Referenced Libraries
Android Dependencies
and make sure that no jar file is repeated.If any jar file is repeated then find it in libs folder of your project and delete it and then refresh the project.
If this does not solve then try:-
Right Click Project>Properties>Java Build path>Order and Export> uncheck android private Libraries.
I added MobFox simple-xml-2.7.1.jar file to Android lib file. When i generated apk i have this error. Delete simple-xml-2.7.1.jar after regenerate apk. I solved my problem by this way.
My Android project contains a plenty of Java libraries that are compiled to dex each time I launch my application. The libraries do not change. Is there any way to precompile those libraries to dex? It make useless work each time compiling the same libraries to dex while making apk.
I had similiar problem and have found a solution, you have to use (undocumented) --incremental switch with dex too. Refer to this solution on SO for more information.
This might be the only current workaround: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html