Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what situation would multiple .dex files be created in Android .apk file?

Tags:

android

apk

dex

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!

like image 346
Kafe Chang Avatar asked Dec 03 '16 09:12

Kafe Chang


1 Answers

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:

Max Dex operations error

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

expanded apk

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.

like image 172
Uddhav P. Gautam Avatar answered Sep 25 '22 20:09

Uddhav P. Gautam