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

Android Overlay机制简述

2014-12-16 16:28 381 查看
事情是这个样子的,由于要打开UItouchsounds,Setting默认的配置都是在frameworks/base/packages/SettingProvider/res/values/defaults.xml中,当我打开的时候发现

<boolname="def_sound_effects_enabled">true</bool>。UItouchsounds配置是true,打开的呀,为什么切换焦点的时候还是没有声音呢。。。。。通过查看源码,也确认是读取的def_sound_effects_enabled配置项。一路坎坷。。。。。。出现这种情况只有一种原因能解释的通了,就是根本没有使用这个配置,也就是使用的是其他地方的配置。然后一路资料查来,原来是我们项目使用了Overlay。由于一直是别人修改的,我才不知道。以前都不知道这个,今天真是长知识了。(Ps:要不是我这么坚持不懈,我肯定不会查到这。感谢我这么的不放弃。人生格言:Never
Giveup!)

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

OverlayMechanism

TheAndroidoverlaymechanismallowstheframeworkandpackageresourcestobecustomizedwithoutchangingthebasepackages.Thecustomizableresoursesfallintothefollowingcategories

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

FordetailedintroductiononAndroidapplicationresources,pleasereferto:

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

1AddOverlayDirectoriesforProduct

1.1ProductOverlaysvsDeviceOverlays

Therearetwotypesofoveralydirectoriesthataffectaproduct:

PRODUCT_PACKAGE_OVERLAYS:usedbyaparticularproduct
DEVICE_PACKAGE_OVERLAYS:usedseveralproductsthatshareacommondevicemodel

ThePRODUCT_PACKAGE_OVERLAYSwilloverridetheDEVICE_PACKAGE_OVERLAYSiftheycontainsameresources,thebehaviorisdefinedby:

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)

1.2Changethemakefiletoaddoverlays

Toaddanoverlaydirectorytoaproduct,changetheproductconfigurationmakefile(forexample:device/vendor-name/device-name/product-name.mk)toaddthefollowinglines:

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)

Ifmultipleoverlaydirectoriesarerequiredtobeadded,separatethemwithblankcharactors.Thedirectoriesdefinedfirstinthelinewilloverridethefollowingdirectoriesiftheycontainthesamesetofresources.

1.3Createresourcesundertheoverlaydirectory

Tooverlaytheresourcesinabasepackage,thesubdirectorycontainingtheoverlayresourcesundertheoverlaydirectory,musthasthesamepathasthebasepackageresourcedirectoryrelativetotheAndroidprojectroot.

Forexample,ifwewanttooverlaytheresourcesunderthebasepackagedirectory:

packages/apps/Settings/res/

weneedtocreatethedirectory:

/packages/apps/Settings/res/

andinthedirectory,puttheoverlayresourcesinthefileswiththesamepathandfilenamesaswheretheyaredefinedinthebasepackage.

Notethat:

Forcolor,bool,string,array,style/themetypes,theresourcevaluesareidentifedbytheirkeys,soforthesetypes,thereisnoneedtoputtheresourcesinafilewiththesamenameasintheoriginalbasepackage.
Forlayout,animation,picturedrawablesandrawtypes,theresourcesareindentifedbytheirfilename,andoverlayfortheseresourcesshouldkeepthefilenamesameasinthebasepackages.

2ChecktheresourceinAPK

Afterchangedtheoverlayresourcesandbuiltthetargetpackages,sometimesweneedtocheckiftheoverlaydirectorytookeffectsandthevaluesarechangedinthefinallygeneratedapks.

2.2UsingAAPT

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.2Usingapktools

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
optionsinthesameorderastheyaredefinedin

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'tdefineaboolvaluewithkey
no_such_key
,
wecanadditintheoverlayfile
bool.xml
likethis:

......


true

......


Ifthe
add-resource
lineismissing,
aapt
toolwillcomplainwhilecreatingtheapkfile:

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

notinthebasepackage;usetoadd.

Anotherwaytoavoidthecomplaintistorun
aapt
withtheoption:

--auto-add-overlay

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