1

I'm trying to understand a few things around how dex files are handled by android.

What I do know is that an APK ships a dex file, the system gets it, optimises it at install time, and stores the resulting ODEX file (optimised dex) at /data/dalvik-cache/myOdexfile and then runs the application from there.

Is that really how it works?

What happens when a dex file downloads and opens a new dex file from the internet? is that optimised as well?

Also - the system, afaik, keeps the original APK around, right? I think that is kept at /data/app/myAPK-1.apk. Is that used in any way by the system? When?

Can you somehow manually force regeneration of the odex file from the stored APK file after installation?

1
  • just like .class file (byte code) is executed in JRE in desktop systems. Android have Dalvik or ART that understands byte code but in .dex format. For android .class file is further converted into .dex file and then apk is formed. Commented Nov 21, 2015 at 12:37

2 Answers 2

2

For general information, you might want to refer to the tag-wiki and the tag-wiki for the tag. But your question even covers some parts not contained there -- and only now I realized that indeed all .apk files seem to include .dex files (I always thought only the "odexed apps" do that), and you seem to be right about what the "O" stands for -- at least it makes much sense. So forgive me, I'm not a developer :)

But still I might be able to answer at least a part of your question.

Yes, your description seems to match how it works (until the question mark, at least). But, to my knowledge, no app should download and install .dex files separately. If not running with root permissions, that would even be difficult (as the app could not write to /data/dalvik-cache, lacking the ). So we can skip the second part as, according to Aristotle's logic, from a wrong assumption one could derive everything :)

Second: Yes, the system keeps the .apk files. If your device has a Custom Recovery installed, you could manually wipe the Dalvik cache from there -- which would force the Android system to re-generate it on its next boot. Basically, this is even done for the (.apk files are located in /system/apps for those, and thus survive a factory-reset), namely when you do a .

Third: Oh, already answered. Yes, by wiping the Dalvik-Cache.

3
  • thanks for your answer. Couple of points though: Wiping the dalvik-cache is a valid point, however that may initiated by the user only on rooted phones, correct? Is there any case a non-rooted device needs to delete the cache, or can the user force the system to do just that, on non-rooted phones? Also, about your other point: Applications have the capability to download and run .dex files from the internet at runtime - although I'm not sure about installing them. I guess they could be downloaded on a temp location in /data/data/myapp/files and be run from there each time.
    – AndroidSec
    Commented Apr 11, 2013 at 12:38
  • Dalvik-Wipe: yes, correct. Custom Recovery Menu, or root via terminal. On non-rooted: only via factory-reset AFAIK. .dex: yes, they can download them -- but not directly execute AFAIK (see yjarabi's answer). They cannot place them into the Dalvik cache, but of course to any place they have access to (their own directories below /data/data/<packagename>, /data/local, etc.).
    – Izzy
    Commented Apr 11, 2013 at 13:52
1

> Is that really how it works?

Yes, I think it works very like that.

> What happens when a dex file downloads and opens a new dex file from the internet? is that optimised as well?

I think it is not possible for an application to open a dex file directly.

> Also - the system, afaik, keeps the original APK around, right? I think that is kept at /data/app/myAPK-1.apk. Is that used in any way by the system? When?

Yes, APK file contains all of assets and resources needed by the app when it runs. So, as an android developer, I'm pretty sure that apk file is used everytime an app runs.

> Can you somehow manually force regeneration of the odex file from the stored APK file after installation?

Yes, if you clear dalvik-cache folder manually (your device should be ROOTed obviously), then system regenerates needed odex files when you run apps. Some apps, like Titanium Backup, has an option to clear dalvik-cache. Also, I remember that there was same option in recovery menu of my device.

1
  • About the second question, apps can run dex files, by using ClassLoader. One example of an app that does it is "DevAppsDirect". One reason for having such functionality is plugins. Anyway, I'd like to ask something too: do apps have any other files that are similar to dex files ? Also, now that Android Lollipop is about to be available, would they still exist? what will replace them, and where will their replacements be found? Some claimed there would be replacements and that it will usually be larger. Is that true? Commented Oct 30, 2014 at 21:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .