Android平台Overlay机制

摘要:
bool array)(2),DEVICE_PACKAGE_OVERLAYS:=设备/供应商名称/设备名称/公用/覆盖$(DEVICE_PAGAGE_OVERAYS)(例如:LOCAL_PATH:=$(LOCAL_PATH)/覆盖如果要定义多个覆盖目录,此路径是Android源目录的相对路径。例如,

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)

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

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

1 为产品添加Overlay目录

1.1 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。

1.2 改变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-name

DEVICE_PACKAGE_OVERLAYS := $(LOCAL_PATH)/overlay

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

1.3 在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.

2 在APK中检测资源

通过overlay改变apk资源文件并生成apk后,一般要检测生成的apk的资源是否已经改变了.

2.1 使用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 使用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.apktoolFor smali/baksmali info, see:http://code.google.com/p/smali/.

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.




免责声明:文章转载自《Android平台Overlay机制》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇FragmentTabHost用法ELK学习笔记之Elasticsearch删除指定日期的数据(脚本+定时任务)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

一个服务器部署多个项目

一:预置条件 1:PHP环境部署完成 2:以Xampp集成环境为例 二:步骤 1:找到\apache\conf目录下的httpd.conf文件     去掉LoadModule vhost_alias_module modules/mod_vhost_alias.so之前的分号(或者#);   把以下两个地方修改为代码对应的路径   DocumentRoo...

如何发挥Visual Studio 2019强大的编辑功能轻松编辑Keil项目

  本文地址:https://www.cnblogs.com/jqdy/p/12565161.html   习惯了VS的强大编辑功能,对Keil 5越来越深恶痛绝。查阅网络文章后按图索骥初步实现了VS编辑Keil项目的功能,但是VS仍然不识别sfr、sbit、bit等特有关键字。虽然不是大问题,但是当键入STC8.h中定义的相关寄存器名称时,不能使用自动提...

快速生成apk 自动发布到网站 便于测试

遇到的问题:    开发者生成的apk 需要不断给 测试安装让他们测试。有没有脚本自动将最新apk上传到服务器,让测试自己安装测试呢?mac电脑 怎么自己搭建文件服务器  启动Tomcat功能在这里不在赘述,有不懂的请你多多百度和Google  然后把你的localhost启用起来!! 不要怂就是干!!! 解决方案: #!/usr/bin/env bash...

ftp命令

ftp命令是标准的文件传输协议的用户接口。ftp是在TCP/IP网络上的计算机之间传输文件的简单有效的方法。它允许用户传输ASCII文件和二进制文件。 在ftp会话过程中,用户可以通过使用ftp客户程序连接到另一台计算机上。从此,用户可以在目录中上下移动、列出目录内容、把文件从远程机拷贝到本地机上、把文件从本地机传输到远程系统中。需要注意的是,如果用户没有...

LNMP平台搭建---MySQL安装篇

  在前两篇中,安装了一个基本的Web服务器,但是只能提供静态网页查看,要做成动态网站,就必须要数据库或其他编程语言支持了,这里先介绍MySQL数据库的安装。   MySQL是一个开源的数据库,在互联网行业应用的很广泛,下面来记录一下从源码安装的步骤,当然,MySQL也有其他安装方式,比如,使用yum下载安装rpm包,或者二进制方式安装,如果机器比较多,可...

TeeChart.v8.01在Delphi下的安装(参考加自己实践修改安装)

参考:http://www.360doc.com/content/10/1012/22/3572432_60499559.shtml 此为参考加自己实践安装说明,其中TeeRecompile.exe编译出错因为没安装含源文件的QuickReport导致,摸索了2天,最后网上下载QuickReport,安装后,不再提示, 未找到QR4D7错误。 一、删除De...