RRO Internals

How RRO works internally

RROs work by leveraging Android's resource resolution system. Here's a breakdown of the internal process:

  1. Resource Resolution: When an app requests a resource (like a string, color, or layout), Android's resource management system is responsible for resolving which resource to use.

  2. Overlay Mechanism: RROs introduce an additional layer in this resolution process. Before returning the original resource, the system checks if there's an overlay package that targets the requesting app and contains a resource with the same name.

  3. Priority System: If multiple overlays provide the same resource, Android uses a priority system to determine which one to use. This is why we set a priority in the manifest file.

  4. Resource Replacement: If a matching overlay resource is found, it's returned instead of the original resource. This happens transparently to the app requesting the resource.

  5. Package Scanning: When an RRO package is installed, the PackageManager scans it and registers it as an overlay. This information is stored in the system, allowing for quick lookup during resource resolution.

  6. Runtime Application: Despite the name "Runtime" Resource Overlay, modern Android versions actually apply most overlays during boot time for performance reasons. However, some overlays can still be toggled on and off at runtime.

  7. Implementation in Framework: The overlay mechanism is implemented deep in the Android framework, specifically in the ResourcesImpl class. This class handles the loading and caching of resources, including the overlay lookup process.

  8. Overlay Manager Service: Android includes an Overlay Manager Service (OMS) that manages the state of overlays. It handles enabling, disabling, and prioritizing overlays.

  9. Idmap Generation: When an overlay is enabled, the system generates an "idmap" file. This file maps resource IDs from the target package to resource IDs in the overlay package, allowing for efficient lookup.

  10. Performance Considerations: To minimize performance impact, Android caches the results of overlay lookups. It also uses the idmap files to quickly determine if an overlay provides a particular resource without having to load the entire overlay package.

This internal mechanism allows Android to provide a flexible theming system without requiring modifications to the target apps. It's a powerful feature, but it's also why RROs typically require system-level permissions to install and enable.

Last updated