Android资源overlay机制

本文详细介绍了Android系统中Overlay机制的使用方法,包括如何通过不修改框架或应用源代码来定制资源,以及如何添加和检查Overlay目录中的资源。此外,还提供了关于资源覆盖的具体实例。
谷歌有一个overlay机制,不修改apk或者framework源代码的情况下,实现资源的定制。
将一篇比较详细的blog粘贴过来。


                    Overlay Mechanism

The Android overlay mechanism allows the framework and package resources to be customized without changing the base packages. The customizable resourses fall into the following categories

  • Configurations (string, bool, bool-array)
  • Localization (string, string-array)
  • UI Appearance (color, drawable, layout, style, theme, animation)
  • Raw resources (audio, video, xml)

For detailed introduction on Android application resources, please refer to:

 http://developer.android.com/guide/topics/resources/available-resources.html

 

1 Add Overlay Directories for Product

1.1 Product Overlays vs Device Overlays

There are two types of overaly directories that affect a product:

  • PRODUCT_PACKAGE_OVERLAYS: used by a particular product
  • DEVICE_PACKAGE_OVERLAYS: used several products that share a common device model

The PRODUCT_PACKAGE_OVERLAYS will override the DEVICE_PACKAGE_OVERLAYS if they contain same resources, the behavior is defined by:

build/core/package.mk (Line: 93)

LOCAL_RESOURCE_DIR := \

  $(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), \

    $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \

  $(wildcard $(foreach dir, $(DEVICE_PACKAGE_OVERLAYS), \

    $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \

  $(LOCAL_RESOURCE_DIR)

 

1.2 Change the makefile to add overlays

To add an overlay directory to a product, change the product configuration makefile (for example: device/vendor-name/device-name/product-name.mk) to add the following lines:

PRODUCT_PACKAGE_OVERLAYS :=  device/vendor-name/device-name/product-name/overlay $(PRODUCT_PACKAGE_OVERLAYS)

 

Or:

 

DEVICE_PACKAGE_OVERLAYS :=  device/vendor-name/device-name/common/overlay $(DEVICE_PACKAGE_OVERLAYS)

 

If multiple overlay directories are required to be added, separate them with blank charactors. The directories defined first in the line will override the following directories if they contain the same set of resources.

 

1.3 Create resources under the overlay directory

To overlay the resources in a base package, the subdirectory containing the overlay resources under the overlay directory, must has the same path as the base package resource directory relative to the Android project root.

For example, if we want to overlay the resources under the base package directory:

packages/apps/Settings/res/

we need to create the directory:

/packages/apps/Settings/res/

and in the directory, put the overlay resources in the files with the same path and file names as where they are defined in the base package.

Note that:

  • For color, bool, string, array, style/theme types, the resource values are identifed by their keys, so for these types, there is no need to put the resources in a file with the same name as in the original base package.
  • For layout, animation, picture drawables and raw types, the resources are indentifed by their file name, and overlay for these resources should keep the file name same as in the base packages.

 


 

2 Check the resource in APK

After changed the overlay resources and built the target packages, sometimes we need to check if the overlay directory took effects and the values are changed in the finally generated apks.

2.2 Using AAPT

 

 

 

Usage:
aapt l[ist] [-v] [-a] file.{zip,jar,apk}
   List contents of Zip-compatible archive.
 
aapt d[ump] [--values] WHAT file.{apk} [asset [asset ...]]
   badging          Print the label and icon for the app declared in APK.
   permissions      Print the permissions from the APK.
   resources        Print the resource table from the APK.
   configurations   Print the configurations in the APK.
   xmltree          Print the compiled xmls in the given assets.
   xmlstrings       Print the strings of the given compiled xml assets.

 

For example:

1. To dump string, bool values:

aapt dump resources Settings.apk

 

2. To dump a raw xml file:

aapt dump xmltree Settings.apk res/xml/appwidget_info.xml

 

3. To dump the current configurations/localizations:

aapt dump configurations Settings.apk

 

2.2 Using apktools

 

Reference: http://code.google.com/p/android-apktool/

 

 

 

 

 

 

 

 

 

Apktool v1.5.0.5a056e3 - a tool for reengineering Android apk files
Copyright 2010 Ryszard Wi??niewski
with smali v1.3.4-ibot8, and baksmali v1.3.4-ibot8
Updated by iBotPeaches
Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 
Usage: apktool [-q|--quiet OR -v|--verbose] COMMAND [...]
 
COMMANDs are:
 
    d[ecode] [OPTS] [
        Decode to
 
        OPTS:
 
        -s, --no-src
            Do not decode sources.
        -r, --no-res
            Do not decode resources.
        -d, --debug
            Decode in debug mode. Check project page for more info.
        -f, --force
            Force delete destination directory.
        -t , --frame-tag
            Try to use framework files tagged by .
        --keep-broken-res
            Use if there was an error and some resources were dropped, e.g.:
            "Invalid config flags detected. Dropping resources", but you
            want to decode them anyway, even with errors. You will have to
            fix them manually before building.
 
    b[uild] [OPTS] [] []
        Build an apk from already decoded application located in .
 
        It will automatically detect, whether files was changed and perform
        needed steps only.
 
        If you omit then current directory will be used.
        If you omit then /dist/
        will be used.
 
        OPTS:
 
        -f, --force-all
            Skip changes detection and build all files.
        -d, --debug
            Build in debug mode. Check project page for more info.
 
    if|install-framework []
        Install framework file to your system.
 
For additional info, see: https://github.com/iBotPeaches/brut.apktool
For smali/baksmali info, see: http://code.google.com/p/smali/

 

2.3 Using dumpres

 


 

3 More on AAPT and Overlay

3.1 How overlay works

While building the package APKs, the overlay directories are passed to aapt command lines using -S options in the same order as they are defined in 

PRODUCT_PACKAGE_OVERLAYS and DEVICE_PACKAGE_OVERLAYS.

For example, while building the Settings APK, the following command are executed:

 

out/host/linux-x86/bin/aapt package -u  -z \
         -M packages/apps/Settings/AndroidManifest.xml \
         -S device/vendor-name/device-name/product-name/overlay/packages/apps/Settings/res \
         -S vendor/vendor-name/media/common/overlay/packages/apps/Settings/res -S packages/apps/Settings/res \
         -I out/target/common/obj/APPS/framework-res_intermediates/package-export.apk \
         --min-sdk-version 16 --target-sdk-version 16 --product default \
         --version-code 16 --version-name 4.1.2-eng.xxxx.20121121.152327 \
         -F out/target/product/product-name/obj/APPS/Settings_intermediates/package.apk

Note: some overlay directories that don't contain the Settings resources will be filtered first, and do not appear in the above command line.

 

3.2 Add extra resources in Overlay

Though not recommanded, we can add new resources in overlay directory, for example, if base package Settings doesn't define a bool value with key no_such_key, we can add it in the overlay file bool.xml like this:

 
  
    ... ...
   
    true
    ... ...
 
 

 

If the add-resource line is missing, aapt tool will complain while creating the apk file:

 

device/vendor-name/device-name/product-name/overlay/packages/apps/Settings/res/values/bools.xml:30: error: Resource at no_such_key appears in overlay but \not in the base package; use to add.

 

Another way to avoid the complaint is to run aapt with the option:

 

--auto-add-overlay
    Automatically add resources that are only in overlays.

<think>好的,用户想了解Android中的Overlay机制,包括学习教程、实现原理和使用方法。首先,我需要回忆一下之前处理过的类似问题和相关的引用资料。根据提供的引用内容,特别是引用1到4,这些资料提到了RRO(Runtime Resource Overlay)、Vendor Overlay、System Overlay以及配置文件和具体实现步骤。 用户的需求可能分为几个部分:机制是什么,如何实现,有什么教程或资源,以及具体的应用场景。我需要先解释Overlay机制的基本概念,然后分类型讨论,比如RRO和Vendor Overlay的区别,接着说明实现原理,比如资源替换的流程,最后给出使用方法,比如如何创建Overlay包和配置。 需要注意的是用户提到的学习教程,可能需要推荐官方文档或实践步骤。同时,引用中的例子涉及Launcher主题替换,这可能是一个实际应用案例,可以举例说明。另外,用户可能对系统层和厂商层的Overlay区别感兴趣,需要明确两者的不同点。 可能还需要考虑用户的实际操作,比如配置文件的位置,代码示例,以及可能遇到的问题,比如权限或启用Overlay的方法。引用4中提到了修改arrays.xml和Shell命令,这些具体步骤需要整理到使用方法中。 需要确保回答结构清晰,分点说明,同时生成相关问题时要覆盖用户可能进一步想了解的内容,比如与其他机制的区别、调试方法、权限控制等。最后,检查引用标注是否正确对应到各个部分,确保信息准确可靠。</think>### Android Overlay机制详解 #### 一、基本概念 Overlay机制Android系统实现资源动态替换的核心技术,通过叠加资源层实现主题定制、多语言支持等功能。主要包含两种实现方式: 1. **RRO(Runtime Resource Overlay)**:运行时动态覆盖资源[^3] 2. **Vendor Overlay**:厂商定制资源方案[^2] #### 二、实现原理 $$资源匹配规则:res/values-<qualifiers>/...$$ 系统通过以下流程加载资源: 1. 解析目标应用资源ID 2. 检查Overlay包中相同资源ID 3. 优先加载Overlay资源 4. 资源映射表更新机制[^4] #### 三、使用方法(以RRO为例) **步骤1:创建Overlay包** ```xml <!-- AndroidManifest.xml --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.theme.overlay"> <overlay android:targetPackage="com.android.launcher" android:priority="1"/> </manifest> ``` **步骤2:配置资源替换** ```bash # 文件结构 res/ ├─ drawable/ │ └─ ic_launcher.xml # 替换目标应用的图标 └─ values/ └─ colors.xml # 修改颜色值 ``` **步骤3:启用Overlay** ```shell adb shell cmd overlay enable --user 0 com.example.theme.overlay ``` #### 四、关键配置文件 1. 目标应用白名单:`frameworks/base/core/res/res/values/arrays.xml` ```xml <string-array name="config_targetPackages"> <item>com.android.systemui</item> <item>com.android.launcher</item> </string-array> ``` 2. Overlay状态存储:`/data/system/overlays.xml`[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值