33

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?

1 Answer 1

45

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
  • 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?) Commented May 8, 2017 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
    Commented May 9, 2017 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. Commented May 9, 2017 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? Commented Apr 27, 2018 at 5:35
  • 1
    @user1788736 Yes. They're left in the APK.
    – iBug
    Commented Apr 27, 2018 at 5:38

You must log in to answer this question.

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