2

I have read the awesome answer on the internals of Android resources by @hackbod : How does the mapping between android resources and resources ID work?

Here is the brief version: The resources, as you may probably know are compiled to a binary format as resources.arsc and are available through the Resources class. These resources are compiled such that they can be easily m-mapped(or mapped to memory).

Now, my question is that are these resources loaded all at once in the memory(presumably during Application process creation) or they are loaded lazily as and when they are required?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
  • AFAIK, they are loaded lazily, but are cached and retained once they are loaded. – CommonsWare Jul 25 '19 at 14:13
  • @CommonsWare I saw that there is a file ApkAssets, that is immutable in memory representation of your APK. That is built once and shared. Can we say that, strings.xml is loaded when that apk-assets object is created, and probably that happens when the first resource whether image, bitmap, font or anything is loaded. – vishal_ratna Jul 05 '22 at 11:21
  • @vishal_ratna: I do not know -- sorry! – CommonsWare Jul 05 '22 at 11:55

1 Answers1

0

What I understand from the code is the resources are eagerly loaded at the time of reading the APK file. There is a file called PackageParser that has a method parseBaseApk. You will observe that while loading the package these things happen, the above method[parseBaseApk] accepts assets as an argument. That comes from an AssetManager that in turn comes from an AssetLoader.

Internally android APK assets, are represented as an AssetAPK array. Each array item could be in-memory representation of asset type FORMAT_ARSC, FORMAT_IDMAP, FORMAT_DIR. Inside APKAssets there is a StringBlock, that represents the compiled string/font resources, and this is created at the time of creation of ApkAssets.

So, I believe the resources are eagerly loaded at the time of loading and parsing the APK, unless I am missing some details out.

vishal_ratna
  • 116
  • 1
  • 5