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
有两种不同的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
为了添加一个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目录,需要用空格隔开.
如果有多个目录,并且都包含同一资源的定义,那么将使用第一个定义的目录中的资源。
想覆盖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.
通过overlay改变apk资源文件并生成apk后,一般要检测生成的apk的资源是否已经改变了.
Forexample:
1.Todumpstring,boolvalues:
2.Todumparawxmlfile:
3.Todumpthecurrentconfigurations/localizations:
Reference:http://code.google.com/p/android-apktool/
另可参考: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)
Fordetailedintroductionon
resources,pleasereferto:
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: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
3MoreonAAPTandOverlayWhilebuildingthepackageAPKs,theoverlaydirectoriesarepassedtoaaptcommandlinesusing
3.1Howoverlayworks-Soptions
inthesameorderastheyaredefinedinPRODUCT_PACKAGE_OVERLAYSandDEVICE_PACKAGE_OVERLAYS.
Forexample,whilebuildingtheSettingsAPK,
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.Thoughnotrecommanded,wecanaddnewresourcesinoverlaydirectory,forexample,ifbasepackage
3.2AddextraresourcesinOverlaySettingsdoesn't
defineaboolvaluewithkeyno_such_key,wecanadditintheoverlayfilebool.xmllike
this:......true......
Iftheadd-resourceline
ismissing,aapttoolwillcomplain
whilecreatingtheapkfile:
device/vendor-name/device-name/product-name/overlay/packages/apps/Settings/res/values/bools.xml:30:error:Resourceatno_such_keyappearsinoverlaybut\
notinthebasepackage;usetoadd.
Anotherwaytoavoidthecomplaintistorunaaptwith
theoption:--auto-add-overlayAutomaticallyaddresourcesthatareonlyinoverlays.
相关文章推荐
- Android数据库——LitePal
- Android事件分发机制(二)
- Android.os包中一些类的使用
- Linphone-android 3.9.1 编译
- android源码解析之(十二)-->系统启动并解析Manifest的流程
- android studio 编译错误 :Duplicate failed copide in APK META-INF/NOTICE.txt解决办法
- Android、iOS支持的音频格式
- android 代码设置editText的 drableRight和drableRight的点击事件
- android 布局间设置分隔线
- Android中XML文件的读写
- Android开发中libs包下面的mips、armeabi、armeabi-v7a和x86
- adb常用命令和logcat使用
- android 常见问题
- android 常见问题汇总
- Android Bitmap换背景颜色
- android jni开发
- Android开发:shape和selector和layer-list的(详细说明)
- Android Studio打包混淆问题总结
- android permission权限与安全机制解析
- Android 基础(二)、开发环境