您的位置:首页 > 移动开发 > Android开发

Android Overlay机制

2016-04-21 10:21 615 查看
转载自(原文未知出处,此为转载地址):http://blog.csdn.net/hanmengaidudu/article/details/39393285

另可参考:http://blog.sina.com.cn/s/blog_645b74b90101ojkc.html

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

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

Configurations(string,bool,bool-array)
Localization(string,string-array)
UIAppearance(color,drawable,layout,style,theme,animation)
Rawresources(audio,video,xml)

FordetailedintroductiononAndroidapplication
resources,pleasereferto:

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


1为产品添加Overlay目录


1.1ProductOverlays与DeviceOverlays

有两种不同的overaly目录定义,来影响最终的效果:

PRODUCT_PACKAGE_OVERLAYS:usedbyaparticularproduct
DEVICE_PACKAGE_OVERLAYS:usedseveralproductsthatshareacommondevicemodel

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

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

LOCAL_RESOURCE_DIR:=\

$(wildcard$(foreachdir,$(PRODUCT_PACKAGE_OVERLAYS),\

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

$(wildcard$(foreachdir,$(DEVICE_PACKAGE_OVERLAYS),\

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

$(LOCAL_RESOURCE_DIR)

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

PRODUCT_PACKAGE_OVERLAYS优先于DEVICE_PACKAGE_OVERLAYS


1.2改变makefile来添加overlays的编译项

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

(forexample: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源码目录的相对路径.

Forexample,如果我们想要替换以下目录的资源文件:

packages/apps/Settings/res/

那么在overlay目录下面必须创建一样的目录:

....../overlay目录/packages/apps/Settings/res/

然后放入想要替换的资源(必须和系统package相同路径和文件名).

注意:

Forcolor,bool,string,array,style/themetypes,theresourcevaluesareidentifedbytheirkeys,soforthesetypes,thereisnoneedtoputtheresourcesinafilewiththesamename
asintheoriginalbasepackage.
Forlayout,animation,picturedrawablesandrawtypes,theresourcesareindentifedbytheirfilename,andoverlayfortheseresourcesshouldkeepthefilenamesameasinthebase
packages.


2在APK中检测资源

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


2.2使用AAPT检测

Usage:

aaptl[ist][-v][-a]file.{zip,jar,apk}

ListcontentsofZip-compatiblearchive.

aaptd[ump][--values]WHATfile.{apk}[asset[asset...]]

badgingPrintthelabelandiconfortheappdeclaredinAPK.

permissionsPrintthepermissionsfromtheAPK.

resourcesPrinttheresourcetablefromtheAPK.

configurationsPrinttheconfigurationsintheAPK.

xmltreePrintthecompiledxmlsinthegivenassets.

xmlstringsPrintthestringsofthegivencompiledxmlassets.


Forexample:

1.Todumpstring,boolvalues:

aaptdumpresourcesSettings.apk


2.Todumparawxmlfile:

aaptdumpxmltreeSettings.apkres/xml/appwidget_info.xml


3.Todumpthecurrentconfigurations/localizations:

aaptdumpconfigurationsSettings.apk





2.2使用apktools检测

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

Apktoolv1.5.0.5a056e3-atoolforreengineeringAndroidapkfiles

Copyright2010RyszardWi??niewski

withsmaliv1.3.4-ibot8,andbaksmaliv1.3.4-ibot8

UpdatedbyiBotPeaches

ApacheLicense2.0(http://www.apache.org/licenses/LICENSE-2.0)


Usage:apktool[-q|--quietOR-v|--verbose]COMMAND[...]


COMMANDsare:


d[ecode][OPTS][


Decodeto



OPTS:


-s,--no-src

Donotdecodesources.

-r,--no-res

Donotdecoderesources.

-d,--debug

Decodeindebugmode.Checkprojectpageformoreinfo.

-f,--force

Forcedeletedestinationdirectory.

-t,--frame-tag

Trytouseframeworkfilestaggedby.

--keep-broken-res

Useiftherewasanerrorandsomeresourcesweredropped,e.g.:

"Invalidconfigflagsdetected.Droppingresources",butyou

wanttodecodethemanyway,evenwitherrors.Youwillhaveto

fixthemmanuallybeforebuilding.


b[uild][OPTS][][]

Buildanapkfromalreadydecodedapplicationlocatedin.


Itwillautomaticallydetect,whetherfileswaschangedandperform

neededstepsonly.


Ifyouomitthencurrentdirectorywillbeused.

Ifyouomitthen/dist/

willbeused.


OPTS:


-f,--force-all

Skipchangesdetectionandbuildallfiles.

-d,--debug

Buildindebugmode.Checkprojectpageformoreinfo.


if|install-framework[]

Installframeworkfiletoyoursystem.


Foradditionalinfo,see:'target='_blank'>https://github.com/iBotPeaches/brut.apktool[/code]
Forsmali/baksmaliinfo,see:'target='_blank'>http://code.google.com/p/smali/[/code]


2.3Usingdumpres


3MoreonAAPTandOverlay


3.1Howoverlayworks

WhilebuildingthepackageAPKs,theoverlaydirectoriesarepassedtoaaptcommandlinesusing
-S
options
inthesameorderastheyaredefinedin

PRODUCT_PACKAGE_OVERLAYS
and
DEVICE_PACKAGE_OVERLAYS
.

Forexample,whilebuildingthe
Settings
APK,
thefollowingcommandareexecuted:

out/host/linux-x86/bin/aaptpackage-u-z\

-Mpackages/apps/Settings/AndroidManifest.xml\

-Sdevice/vendor-name/device-name/product-name/overlay/packages/apps/Settings/res\

-Svendor/vendor-name/media/common/overlay/packages/apps/Settings/res-Spackages/apps/Settings/res\

-Iout/target/common/obj/APPS/framework-res_intermediates/package-export.apk\

--min-sdk-version16--target-sdk-version16--productdefault\

--version-code16--version-name4.1.2-eng.xxxx.20121121.152327\

-Fout/target/product/product-name/obj/APPS/Settings_intermediates/package.apk


Note:
someoverlaydirectoriesthatdon'tcontaintheSettingsresourceswillbefilteredfirst,anddonotappearintheabovecommandline.


3.2AddextraresourcesinOverlay

Thoughnotrecommanded,wecanaddnewresourcesinoverlaydirectory,forexample,ifbasepackage
Settings
doesn't
defineaboolvaluewithkey
no_such_key
,wecanadditintheoverlayfile
bool.xml
like
this:

......

true

......


Ifthe
add-resource
line
ismissing,
aapt
toolwillcomplain
whilecreatingtheapkfile:

device/vendor-name/device-name/product-name/overlay/packages/apps/Settings/res/values/bools.xml:30:error:Resourceatno_such_keyappearsinoverlaybut\

notinthebasepackage;usetoadd.

Anotherwaytoavoidthecomplaintistorun
aapt
with
theoption:

--auto-add-overlay

Automaticallyaddresourcesthatareonlyinoverlays.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: