README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. Android Utility Function Library
  2. ================================
  3. If you need a feature that is native to Linux but not present on other
  4. platforms, construct a platform-dependent implementation that shares
  5. the Linux interface. That way the actual device runs as "light" as
  6. possible.
  7. If that isn't feasible, create a system-independent interface and hide
  8. the details.
  9. The ultimate goal is *not* to create a super-duper platform abstraction
  10. layer. The goal is to provide an optimized solution for Linux with
  11. reasonable implementations for other platforms.
  12. Resource overlay
  13. ================
  14. Introduction
  15. ------------
  16. Overlay packages are special .apk files which provide no code but
  17. additional resource values (and possibly new configurations) for
  18. resources in other packages. When an application requests resources,
  19. the system will return values from either the application's original
  20. package or any associated overlay package. Any redirection is completely
  21. transparent to the calling application.
  22. Resource values have the following precedence table, listed in
  23. descending precedence.
  24. * overlay package, matching config (eg res/values-en-land)
  25. * original package, matching config
  26. * overlay package, no config (eg res/values)
  27. * original package, no config
  28. During compilation, overlay packages are differentiated from regular
  29. packages by passing the -o flag to aapt.
  30. Background
  31. ----------
  32. This section provides generic background material on resources in
  33. Android.
  34. How resources are bundled in .apk files
  35. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. Android .apk files are .zip files, usually housing .dex code,
  37. certificates and resources, though packages containing resources but
  38. no code are possible. Resources can be divided into the following
  39. categories; a `configuration' indicates a set of phone language, display
  40. density, network operator, etc.
  41. * assets: uncompressed, raw files packaged as part of an .apk and
  42. explicitly referenced by filename. These files are
  43. independent of configuration.
  44. * res/drawable: bitmap or xml graphics. Each file may have different
  45. values depending on configuration.
  46. * res/values: integers, strings, etc. Each resource may have different
  47. values depending on configuration.
  48. Resource meta information and information proper is stored in a binary
  49. format in a named file resources.arsc, bundled as part of the .apk.
  50. Resource IDs and lookup
  51. ~~~~~~~~~~~~~~~~~~~~~~~
  52. During compilation, the aapt tool gathers application resources and
  53. generates a resources.arsc file. Each resource name is assigned an
  54. integer ID 0xppttiii (translated to a symbolic name via R.java), where
  55. * pp: corresponds to the package namespace (details below).
  56. * tt: corresponds to the resource type (string, int, etc). Every
  57. resource of the same type within the same package has the same
  58. tt value, but depending on available types, the actual numerical
  59. value may be different between packages.
  60. * iiii: sequential number, assigned in the order resources are found.
  61. Resource values are specified paired with a set of configuration
  62. constraints (the default being the empty set), eg res/values-sv-port
  63. which imposes restrictions on language (Swedish) and display orientation
  64. (portrait). During lookup, every constraint set is matched against the
  65. current configuration, and the value corresponding to the best matching
  66. constraint set is returned (ResourceTypes.{h,cpp}).
  67. Parsing of resources.arsc is handled by ResourceTypes.cpp; this utility
  68. is governed by AssetManager.cpp, which tracks loaded resources per
  69. process.
  70. Assets are looked up by path and filename in AssetManager.cpp. The path
  71. to resources in res/drawable are located by ResourceTypes.cpp and then
  72. handled like assets by AssetManager.cpp. Other resources are handled
  73. solely by ResourceTypes.cpp.
  74. Package ID as namespace
  75. ~~~~~~~~~~~~~~~~~~~~~~~
  76. The pp part of a resource ID defines a namespace. Android currently
  77. defines two namespaces:
  78. * 0x01: system resources (pre-installed in framework-res.apk)
  79. * 0x7f: application resources (bundled in the application .apk)
  80. ResourceTypes.cpp supports package IDs between 0x01 and 0x7f
  81. (inclusive); values outside this range are invalid.
  82. Each running (Dalvik) process is assigned a unique instance of
  83. AssetManager, which in turn keeps a forest structure of loaded
  84. resource.arsc files. Normally, this forest is structured as follows,
  85. where mPackageMap is the internal vector employed in ResourceTypes.cpp.
  86. mPackageMap[0x00] -> system package
  87. mPackageMap[0x01] -> NULL
  88. mPackageMap[0x02] -> NULL
  89. ...
  90. mPackageMap[0x7f - 2] -> NULL
  91. mPackageMap[0x7f - 1] -> application package
  92. The resource overlay extension
  93. ------------------------------
  94. The resource overlay mechanism aims to (partly) shadow and extend
  95. existing resources with new values for defined and new configurations.
  96. Technically, this is achieved by adding resource-only packages (called
  97. overlay packages) to existing resource namespaces, like so:
  98. mPackageMap[0x00] -> system package -> system overlay package
  99. mPackageMap[0x01] -> NULL
  100. mPackageMap[0x02] -> NULL
  101. ...
  102. mPackageMap[0x7f - 2] -> NULL
  103. mPackageMap[0x7f - 1] -> application package -> overlay 1 -> overlay 2
  104. The use of overlay resources is completely transparent to
  105. applications; no additional resource identifiers are introduced, only
  106. configuration/value pairs. Any number of overlay packages may be loaded
  107. at a time; overlay packages are agnostic to what they target -- both
  108. system and application resources are fair game.
  109. The package targeted by an overlay package is called the target or
  110. original package.
  111. Resource overlay operates on symbolic resources names. Hence, to
  112. override the string/str1 resources in a package, the overlay package
  113. would include a resource also named string/str1. The end user does not
  114. have to worry about the numeric resources IDs assigned by aapt, as this
  115. is resolved automatically by the system.
  116. As of this writing, the use of resource overlay has not been fully
  117. explored. Until it has, only OEMs are trusted to use resource overlay.
  118. For this reason, overlay packages must reside in /system/overlay.
  119. Resource ID mapping
  120. ~~~~~~~~~~~~~~~~~~~
  121. Resource identifiers must be coherent within the same namespace (ie
  122. PackageGroup in ResourceTypes.cpp). Calling applications will refer to
  123. resources using the IDs defined in the original package, but there is no
  124. guarantee aapt has assigned the same ID to the corresponding resource in
  125. an overlay package. To translate between the two, a resource ID mapping
  126. {original ID -> overlay ID} is created during package installation
  127. (PackageManagerService.java) and used during resource lookup. The
  128. mapping is stored in /data/resource-cache, with a @idmap file name
  129. suffix.
  130. The idmap file format is documented in a separate section, below.
  131. Package management
  132. ~~~~~~~~~~~~~~~~~~
  133. Packages are managed by the PackageManagerService. Addition and removal
  134. of packages are monitored via the inotify framework, exposed via
  135. android.os.FileObserver.
  136. During initialization of a Dalvik process, ActivityThread.java requests
  137. the process' AssetManager (by proxy, via AssetManager.java and JNI)
  138. to load a list of packages. This list includes overlay packages, if
  139. present.
  140. When a target package or a corresponding overlay package is installed,
  141. the target package's process is stopped and a new idmap is generated.
  142. This is similar to how applications are stopped when their packages are
  143. upgraded.
  144. Creating overlay packages
  145. -------------------------
  146. Overlay packages should contain no code, define (some) resources with
  147. the same type and name as in the original package, and be compiled with
  148. the -o flag passed to aapt.
  149. The aapt -o flag instructs aapt to create an overlay package.
  150. Technically, this means the package will be assigned package id 0x00.
  151. There are no restrictions on overlay packages names, though the naming
  152. convention <original.package.name>.overlay.<name> is recommended.
  153. Example overlay package
  154. ~~~~~~~~~~~~~~~~~~~~~~~
  155. To overlay the resource bool/b in package com.foo.bar, to be applied
  156. when the display is in landscape mode, create a new package with
  157. no source code and a single .xml file under res/values-land, with
  158. an entry for bool/b. Compile with aapt -o and place the results in
  159. /system/overlay by adding the following to Android.mk:
  160. LOCAL_AAPT_FLAGS := -o com.foo.bar
  161. LOCAL_MODULE_PATH := $(TARGET_OUT)/overlay
  162. The ID map (idmap) file format
  163. ------------------------------
  164. The idmap format is designed for lookup performance. However, leading
  165. and trailing undefined overlay values are discarded to reduce the memory
  166. footprint.
  167. idmap grammar
  168. ~~~~~~~~~~~~~
  169. All atoms (names in square brackets) are uint32_t integers. The
  170. idmap-magic constant spells "idmp" in ASCII. Offsets are given relative
  171. to the data_header, not to the beginning of the file.
  172. map := header data
  173. header := idmap-magic <crc32-original-pkg> <crc32-overlay-pkg>
  174. idmap-magic := <0x706d6469>
  175. data := data_header type_block+
  176. data_header := <m> header_block{m}
  177. header_block := <0> | <type_block_offset>
  178. type_block := <n> <id_offset> entry{n}
  179. entry := <resource_id_in_target_package>
  180. idmap example
  181. ~~~~~~~~~~~~~
  182. Given a pair of target and overlay packages with CRC sums 0x216a8fe2
  183. and 0x6b9beaec, each defining the following resources
  184. Name Target package Overlay package
  185. string/str0 0x7f010000 -
  186. string/str1 0x7f010001 0x7f010000
  187. string/str2 0x7f010002 -
  188. string/str3 0x7f010003 0x7f010001
  189. string/str4 0x7f010004 -
  190. bool/bool0 0x7f020000 -
  191. integer/int0 0x7f030000 0x7f020000
  192. integer/int1 0x7f030001 -
  193. the corresponding resource map is
  194. 0x706d6469 0x216a8fe2 0x6b9beaec 0x00000003 \
  195. 0x00000004 0x00000000 0x00000009 0x00000003 \
  196. 0x00000001 0x7f010000 0x00000000 0x7f010001 \
  197. 0x00000001 0x00000000 0x7f020000
  198. or, formatted differently
  199. 0x706d6469 # magic: all idmap files begin with this constant
  200. 0x216a8fe2 # CRC32 of the resources.arsc file in the original package
  201. 0x6b9beaec # CRC32 of the resources.arsc file in the overlay package
  202. 0x00000003 # header; three types (string, bool, integer) in the target package
  203. 0x00000004 # header_block for type 0 (string) is located at offset 4
  204. 0x00000000 # no bool type exists in overlay package -> no header_block
  205. 0x00000009 # header_block for type 2 (integer) is located at offset 9
  206. 0x00000003 # header_block for string; overlay IDs span 3 elements
  207. 0x00000001 # the first string in target package is entry 1 == offset
  208. 0x7f010000 # target 0x7f01001 -> overlay 0x7f010000
  209. 0x00000000 # str2 not defined in overlay package
  210. 0x7f010001 # target 0x7f010003 -> overlay 0x7f010001
  211. 0x00000001 # header_block for integer; overlay IDs span 1 element
  212. 0x00000000 # offset == 0
  213. 0x7f020000 # target 0x7f030000 -> overlay 0x7f020000