Android Overlay机制

来源:互联网 发布:翻墙软件免费下载 编辑:程序博客网 时间:2024/06/08 07:09

Android overlay 机制允许在不修改 packages 中 apk 的情况下,来自定义 framework 和 package 中的资源文件,实现资源的定制。来达到显示不同的 UI 得目的(如MIUI)。

以下几类能够通过该机制定义:

  1. Configurations (string, bool, bool-array)
  2. Localization (string, string-array)
  3. UI Appearance (color, drawable, layout, style, theme, animation)
  4. Raw resources (audio, video, xml)

为产品添加Overlay目录
Product Overlays与Device Overlays
有两种不同的overaly目录定义,来影响最终的效果:
PRODUCT_PACKAGE_OVERLAYS: used by a particular product

DEVICE_PACKAGE_OVERLAYS: used several products that share a common device model

如果包含同一资源,那么 PRODUCT_PACKAGE_OVERLAYS 将覆盖 DEVICE_PACKAGE_OVERLAYS 中的, 这两个定义如下:

build/core/package.mk (Line: 93)1 LOCAL_RESOURCE_DIR := /2 $(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), /3 $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) /4 $(wildcard $(foreach dir, $(DEVICE_PACKAGE_OVERLAYS), /5 $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) /6 $(LOCAL_RESOURCE_DIR)

PRODUCT_PACKAGE_OVERLAYS & DEVICE_PACKAGE_OVERLAYS 功能是一样的,只是优先级不一样:PRODUCT_PACKAGE_OVERLAYS 优先于 DEVICE_PACKAGE_OVERLAYS。

改变makefile来添加overlays的编译项

为了添加一个overlay目录, 需要修改产品的makefile:

(for example: device/vendor-name/device-name/product-name.mk)

添加以下几行:

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)

(如: device/vendor-name/device-name/device_base.mk)中添加:

LOCAL_PATH := device/vendor-name/device-nameDEVICE_PACKAGE_OVERLAYS := $(LOCAL_PATH)/overlay

如果要定义多个overlays目录,需要用空格隔开。如果有多个目录,并且都包含同一资源的定义,那么将使用第一个定义的目录中的资源。

在overlay目录下创建资源文件
想覆盖Android系统自带package中资源文件, 那么在overlay目录下必须包含和要替换package相同的路径, 该路径是Android源码目录的相对路径.
For example, 如果我们想要替换以下目录的资源文件:
packages/apps/Settings/res/
那么在overlay目录下面必须创建一样的目录:
……/overlay目录/packages/apps/Settings/res/
然后放入想要替换的资源(必须和系统package相同路径和文件名)。
注意:
(1),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.

(2),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.

在APK中检测资源
通过overlay改变apk资源文件并生成apk后,一般要检测生成的apk的资源是否已经改变了.
使用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

0 0
原创粉丝点击