本文转载自:http://blog.csdn.net/wh_19910525/article/details/39254815

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机制【转】的更多相关文章

  1. Android的Overlay机制

    相关知识点的掌握: AAPT的使用和原理 编译脚本 参考:http://blog.sina.com.cn/s/blog_645b74b90101ojkc.html

  2. Android平台的事件处理机制和手指滑动例子

    Android平台的事件处理机制有两种 基于回调机制的事件处理:Android平台中,每个View都有自己的处理事件的回调方法,开发人员可以通过重写View中的这些回调方法来实现需要的响应事件. 基于 ...

  3. Android 平台 Native 代码的崩溃捕获机制及实现

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/g-WzYF3wWAljok1XjPoo7w 一.背景 ...

  4. 4412开发板Android教程——Android平台简介

    本文转自迅为开发板论坛:http://www.topeetboard.com Android和IOS Android的历史 Android公司 2005年Google收购成立22个月的Android公 ...

  5. android平台的技术架构

    Android平台采用了软件堆层(Software Stack)的架构,主要分为四个部分: 1.应用软件 Android 连同一个核心应用程序包一起发布,该应用程序包包括E-mail客户端.SMS短消 ...

  6. android设备休眠机制

    如果一开始就对Android手机的硬件架构有一定的了解,设计出的应用程序通常不会成为待机电池杀手,而要设计出正确的通信机制与通信协议也并不困难.但如果不去了解而盲目设计,可就没准了. 首先Androi ...

  7. Android事件传递机制(转)

    Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和多指操作.所有这些都构成了Android中的事件响应.总的来说,所有的事件都 ...

  8. Android内存管理机制

    相信一步步走过来的Android从业者,每个人都会遇到OOM的情况.如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力. 今天我们就谈谈在Android平台下内存的管理之道,开始 ...

  9. Android的事件处理机制(一)------基于回调机制的事件处理

    Android平台的事件处理机制有两种,一种是基于回调机制的,一种是基于监听接口的,现介绍第一种:基于回调机制的事件处理.Android平台中,每个View都有自己的处理事件的回调方法,开发人员可以通 ...

随机推荐

  1. Spring Boot (10) mybatis三种动态sql

    脚本SQL xml配置方式见mybatis讲解,下面是用<script>的方式把它照搬过来,用注解来实现.适于xml配置转换到注解配置 @Select("<script&g ...

  2. SQLServer2008 使用BCP导入导出表数据

    --先开启cmdshell EXEC sp_configure 'show advanced options', 1 GO RECONFIGURE GO EXEC sp_configure 'xp_c ...

  3. [ller必读] LoveLive! 必备技能之 Python Pillow 自动处理截图

    起因 喜欢的歌,静静地听:喜欢的人,远远的看.30天前,就是3月14号,我情不自禁地走近了<LoveLive!学院偶像祭>,这是我的第一张卡片(见下图).第二天也就是3月15日,海未生日了 ...

  4. Deutsch lernen (06)

    1. das Verzeichnis,-se 表格:名单,目录 Die Daten sind in einem Verzeichnis enthalten. (包括,含有) 2. enthalten  ...

  5. MyEclipse 连接Oracle数据库(初学者必看)

    前言:刚接触Oracle数据库,便有一个需求,编写控制台程序,实现主人登录.数据库为Oracle.下面详细介绍一下MyEclipse 连接Oracle数据库.   package DbHelp; im ...

  6. CorelDRAW记事本写实图标的制作流程

    本篇教程用CorelDRAW快速制作记事本写实图标,在制作的过程中主要使用了位图填充和金属材质的实现,加之一些常用工具的用法处理,最后将对象剪裁至图文框就好了,现在跟小编一起来看看详细的操作吧! 使用 ...

  7. Java继承实现接口的抽象类

    1.TestIntace.java package com.chase.abstrac; /** * 接口 * @author Chase * * @date 2013-10-21 下午02:29:1 ...

  8. Scrapy处理200-300范围之外的响应代码

    HttpErrorMiddleware 类scrapy.spidermiddlewares.httperror.HttpErrorMiddleware 过滤掉不成功(错误)的HTTP响应,以便蜘蛛不必 ...

  9. [adb]查看 App的appPackage和appActivity

    最近在写app的UI框架,写脚本之前需要知道app的包名和activity,如果获取呢: 需求配置abdrioid sdk环境 方法1:abd log 1. 打开cmd命令窗口2.在命令窗口中输入,a ...

  10. 【剑指Offer】30、连续子数组的最大和

      题目描述:   HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是 ...