31

Android keeps APK files of installed user apps in /data/app directory and system apps in /system/app directory.

Does it mean Android extracts the dex files from the APK files every time the app is launched? If not, can I safely delete the APK files and still run the app?

43

An Android APK usually contains these things.

assets/
lib/
META-INF/
res/
AndroidManifest.xml
classes.dex

Upon installation, the APK file is copied to /data/app, and classes.dex is extracted and "optimized" by running dex2oat on it (on Android 5+ lib/ is also extracted). Result of the optimization is stored in /data/dalvik-cache/ so an app needs to be optimized only once per installation or update. Everything else is kept inside the APK. So the first answer is very clear: Things like assets and res that's required by the app must be provided, and they are inside the APK. The APK file is kept for supporting purposes. If you delete an APK, the app definitely won't start at all. (App: Where's my assets?)

Second, Google Play added support for "Delta Update" very long ago. In the delta update procedure, the difference between the old package and the new package is calculated. Then GP downloads the "Delta" and applies changes to the original APK to yield the updated APK, thus reducing download size.

An APK is always signed. This can prevent malicious modification to the package. You definitely don't want to install a modded app without knowing what is changed, or whether a virus has been injected. The META-INF/ inside the APK works for this purpose. Unofficial changes will result in non matching signature, and the Android system will refuse to install the modded app.

Also, when you update your Android OS, all dex files are "optimized" again so that you don't need to re-install them one by one. As is said above, optimization requires classes.dex file from the original package.

  • 5
    Note that Google Play Store's delta update abilities depend on having the APK to compute the delta between the installed and the updated versions, and the subsequent patching. Plus, the APK is compressed so it saves some space (anyone remember ye olde ZipAlign?) – Tamoghna Chowdhury May 8 '17 at 19:02
  • @Tamoghna Space isn't really a problem, otherwise Android 5+ wouldn't have extracted lib/ from APK, iOS wouldn't have extracted everything from IPA. It's just for basic integrity check. – iBug May 9 '17 at 2:40
  • Executables rarely compress well - the space saving is more related to compressed resources like the binarized XML files, which aren't extracted. Also, about that ZipAlign - resource access can be somewhat faster on optimised (read word-aligned) compressed files. – Tamoghna Chowdhury May 9 '17 at 5:03
  • 1
    So the asset files such as html and js files are not copied from apk to a specific location in android file system? – user1788736 Apr 27 '18 at 5:35
  • 1
    @user1788736 Yes. They're left in the APK. – iBug Apr 27 '18 at 5:38

protected by Community Apr 13 at 9:38

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.