Android and DEX files handling at runtime 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?
For general information, you might want to refer to the odex tag-wiki and the tag-wiki for the dalvik 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 file-permissions). 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 system-apps (.apk files are located in /system/apps for those, and thus survive a factory-reset), namely when you do a factory-reset.

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