2

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?

ajma
  • 12,106
  • 12
  • 71
  • 90
AndroidSec
  • 299
  • 1
  • 5
  • 13

2 Answers2

3

Yes, Android creates ODEXs from DEXs at install-time.

Yes, the APK is saved. DEX and ODEX only contain code. The APK is where all your resources and assets come from - your label, your icon, your layouts, etc.

To my understanding DEX optimization is purely an install-time process, and is not performed on any DEX that is not part of an installed APK. And since a DEX that is part of an APK cannot change without installing a new APK, there is no need to regenerate an ODEX outside of the normal install process.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • Alright, so why is the APK kept around in the system in /data/app directory? It's never used for anything else? AFAIK the resources are extracted in /data/data/nameoftheapp/ and are loaded from there at runtime, not from the apk. – AndroidSec Apr 10 '13 at 12:20
  • No, only files the app itself places in /data/data/packagename/ go in that directory. App resources and assets that the app does not extract do not get extracted. Most resources and assets in an APK file can be used in-place and do not need to be extracted. – j__m Apr 10 '13 at 12:21
  • My app https://play.google.com/store/apps/details?id=by4a.reflect will show you just some of the resources and assets stored inside APK files. – j__m Apr 10 '13 at 12:49
  • Alright. So I change my question: The .dex and .so files in the APK remain in the stored APK even though they are extracted and used from other directories. Do the original ones ever get used again in the lifetime of the system before you uninstall the application? – AndroidSec Apr 10 '13 at 14:44
  • Well, I believe that when ODEX files were first introduced to Android, the DEX files were optimized when the system was updated. It's possible another system update may optimize them again (for example, if there's an improved optimization algorithm). I don't know of any situation where embedded .so files could possibly be used for anything. They're native code, so all there is to do with them is extract them to separate files so the underlying Linux system can load them. (SO files cannot be loaded from anywhere except separate files, thus the extraction.) – j__m Apr 10 '13 at 14:51
  • 1
    FYI you should avoid changing your questions. If a question is resolved, you should accept the answer. If you have another question, you should post it separately. – j__m Apr 10 '13 at 14:52
  • Actually, I've seen a number of devices which re-optimize everything on startup of the Android runtime. Perhaps they do that, and keep the original .dex files around, so that if a platform component which would be resolved in the .odex changes in an upgrade, the installed apk's can be updated to work with the reshuffled contents of the new version, rather than being broken by the change. – Chris Stratton Jun 13 '13 at 19:33
1

When you load a dex file dynamically, dalvik will optimize it the first time it is loaded. You have to pass a directory that you application can write to, for it to place the cached odex in, typically within the application's data dir.

JesusFreke
  • 19,784
  • 5
  • 65
  • 68