在 Android 8.0 的时候,Android 项目可以动态的加载资源而可以不用再overlay 中去静态处理,这样就可以方便的对 Android 资源进行处理了。
例如:
1. -- Android.mk
LOCAL_PATH := $(call my-dir)
$(warning mike LOCAL_PATH = $(LOCAL_PATH))
include $(CLEAR_VARS)
LOCAL_RESOURCE_DIR := \
$(LOCAL_PATH)/res \
$(warning mike LOCAL_RESOURCE_DIR = $(LOCAL_RESOURCE_DIR))
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/overlay
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_SDK_VERSION := current
# 用于当前目录的命名
LOCAL_PACKAGE_NAME := LauncherOverlay
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_MANIFEST_FILE := AndroidManifest.xml
include $(BUILD_PACKAGE)
2. -- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.launcher3overlay">
<overlay android:targetPackage="com.android.launcher3" android:priority="1" android:isStatic="true"/>
</manifest>
3. 直接在 res 中添加对应的资源。编译完成后,会生成一个 apk。
动态加载资源 RRO 具体实现在 com.android.server.om
,看看官方给的表述
用于管理 asset overlay 的服务。Asset overlays 来自于系统和 app apks 加载的额外资源。这个服务(OMS), 跟踪已经被安装了要使用的 overlay,提供了更改此资源的方法。在 Activity 的运行生命周期中,对正在运行的应用去更改它的资源。允许应用重新读取 overlay 的资源。OMS本身不会改变 overlay。相反,它只负责确保在技术和安全的角度,overlays 可以在外部的请求下被激活。在组件中这种打开或关闭 overlay 的方式能在不同的用例中被使用,如 主题或者是动态定制。
OMS从三个来源接收信息:
- 从 SystemService 类回调,特别是当 Android 框架正在启动以及最终用户切换 Android 用户时。
- 来至 PMS 的 Intent。Overlay 是一个常规的 apk,无论这个应用被安装,卸载等操作时,PMS 都会通过发送广播意图。当 OMS 接收到这些意图时,OMS 更新可用 overlay 的内部表示。如果这是一个可见的改变,将在受影响的 apk 中重新触发 asset 的更新。
- 通过 @link ioverlaymanager aidl 接口的外部请求。这个接口允许客户机去读取关于当前可用 overlay 的信息,更新一个 overlay 或者是相关的 order。如果请求的目标是与调用者运行相同的Android用户,或者调用者拥有“跨用户交互”的完全权限,则授予读取访问权限。如果调用方被授予读访问权限,并且另外还拥有更改覆盖包权限,则授予写访问权限。
英语不好,附上英语表述
/**
* Service to manage asset overlays.
*
* <p>Asset overlays are additional resources that come from apks loaded
* alongside the system and app apks. This service, the OverlayManagerService
* (OMS), tracks which installed overlays to use and provides methods to change
* this. Changes propagate to running applications as part of the Activity
* lifecycle. This allows Activities to reread their resources at a well
* defined point.</p>
*
* <p>By itself, the OMS will not change what overlays should be active.
* Instead, it is only responsible for making sure that overlays *can* be used
* from a technical and security point of view and to activate overlays in
* response to external requests. The responsibility to toggle overlays on and
* off lies within components that implement different use-cases such as themes
* or dynamic customization.</p>
*
* <p>The OMS receives input from three sources:</p>
*
* <ul>
* <li>Callbacks from the SystemService class, specifically when the
* Android framework is booting and when the end user switches Android
* users.</li>
*
* <li>Intents from the PackageManagerService (PMS). Overlays are regular
* apks, and whenever a package is installed (or removed, or has a
* component enabled or disabled), the PMS broadcasts this as an intent.
* When the OMS receives one of these intents, it updates its internal
* representation of the available overlays and, if there was a visible
* change, triggers an asset refresh in the affected apps.</li>
*
* <li>External requests via the {@link IOverlayManager AIDL interface}.
* The interface allows clients to read information about the currently
* available overlays, change whether an overlay should be used or not, and
* change the relative order in which overlay packages are loaded.
* Read-access is granted if the request targets the same Android user as
* the caller runs as, or if the caller holds the
* INTERACT_ACROSS_USERS_FULL permission. Write-access is granted if the
* caller is granted read-access and additionaly holds the
* CHANGE_OVERLAY_PACKAGES permission.</li>
* </ul>
*
* <p>The AIDL interface works with String package names, int user IDs, and
* {@link OverlayInfo} objects. OverlayInfo instances are used to track a
* specific pair of target and overlay packages and include information such as
* the current state of the overlay. OverlayInfo objects are immutable.</p>
*
* <p>Internally, OverlayInfo objects are maintained by the
* OverlayManagerSettings class. The OMS and its helper classes are notified of
* changes to the settings by the OverlayManagerSettings.ChangeListener
* callback interface. The file /data/system/overlays.xml is used to persist
* the settings.</p>
*
* <p>Creation and deletion of idmap files are handled by the IdmapManager
* class.</p>
*
* <p>The following is an overview of OMS and its related classes. Note how box
* (2) does the heavy lifting, box (1) interacts with the Android framework,
* and box (3) replaces box (1) during unit testing.</p>
*
* <pre>
* Android framework
* | ^
* . . . | . . . . | . . . .
* . | | .
* . AIDL, broadcasts .
* . intents | .
* . | | . . . . . . . . . . . .
* . v | . .
* . OverlayManagerService . OverlayManagerTests .
* . \ . / .
* . (1) \ . / (3) .
* . . . . . . . . . . \ . . . / . . . . . . . . .
* . \ / .
* . (2) \ / .
* . OverlayManagerServiceImpl .
* . | | .
* . | | .
* . OverlayManagerSettings IdmapManager .
* . .
* . . . . . . . . . . . . . . . . . . . . . .
* </pre>
*
* <p>Finally, here is a list of keywords used in the OMS context.</p>
*
* <ul>
* <li><b>target [package]</b> -- A regular apk that may have its resource
* pool extended by zero or more overlay packages.</li>
*
* <li><b>overlay [package]</b> -- An apk that provides additional
* resources to another apk.</li>
*
* <li><b>OMS</b> -- The OverlayManagerService, i.e. this class.</li>
*
* <li><b>approved</b> -- An overlay is approved if the OMS has verified
* that it can be used technically speaking (its target package is
* installed, at least one resource name in both packages match, the
* idmap was created, etc) and that it is secure to do so. External
* clients can not change this state.</li>
*
* <li><b>not approved</b> -- The opposite of approved.</li>
*
* <li><b>enabled</b> -- An overlay currently in active use and thus part
* of resource lookups. This requires the overlay to be approved. Only
* external clients can change this state.</li>
*
* <li><b>disabled</b> -- The opposite of enabled.</li>
*
* <li><b>idmap</b> -- A mapping of resource IDs between target and overlay
* used during resource lookup. Also the name of the binary that creates
* the mapping.</li>
* </ul>
*/