我是靠谱客的博主 飘逸盼望,最近开发中收集的这篇文章主要介绍Android点滴积累系列--资源overlay机制(转),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

谷歌有一个overlay机制,不修改apk或者framework源代码的情况下,实现资源的定制。

将一篇比较详细的blog粘贴过来。

http://mmmyddd.freeshell.net/wiki/android/overlay.html

                 OverlayMechanism

The Android overlay mechanism allows the frameworkand package resources to be customized without changing the basepackages. The customizable resourses fall into the followingcategories

  • Configurations (string, bool, bool-array)
  • Localization (string, string-array)
  • UI Appearance (color, drawable, layout, style, theme,animation)
  • Raw resources (audio, video, xml)

For detailed introduction on Android applicationresources, please refer to:

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

 

1 Add Overlay Directoriesfor Product

1.1 Product Overlays vsDevice Overlays

There are two types of overaly directories thataffect a product:

  • PRODUCT_PACKAGE_OVERLAYS: used by a particularproduct
  • DEVICE_PACKAGE_OVERLAYS: used several products that share acommon device model

The PRODUCT_PACKAGE_OVERLAYS will override theDEVICE_PACKAGE_OVERLAYS if they contain same resources, thebehavior is defined by:

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

LOCAL_RESOURCE_DIR :=

  $(wildcard $(foreach dir,$(PRODUCT_PACKAGE_OVERLAYS),

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

  $(wildcard $(foreach dir,$(DEVICE_PACKAGE_OVERLAYS),

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

 $(LOCAL_RESOURCE_DIR)

 

1.2 Change the makefile to add overlays

To add an overlay directory to a product, changethe product configuration makefile (for example: device/vendor-name/device-name/product-name.mk) to add the followinglines:

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)

 

If multiple overlay directories are required to beadded, separate them with blank charactors. The directories definedfirst in the line will override the following directories if theycontain the same set of resources.

 

1.3 Create resources underthe overlay directory

To overlay the resources in a base package, thesubdirectory containing the overlay resources under the overlaydirectory, must has the same path as the base package resourcedirectory relative to the Android project root.

For example, if we want to overlay the resourcesunder the base package directory:

packages/apps/Settings/res/

we need to create the directory:

/packages/apps/Settings/res/

and in the directory, put the overlay resources inthe files with the same path and file names as where they aredefined in the base package.

Note that:

  • For color, bool, string, array, style/theme types, theresource values are identifed by their keys, so for these types,there is no need to put the resources in a file with the same nameas in the original base package.
  • For layout, animation, picture drawables and raw types, theresources are indentifed by their file name, and overlay for theseresources should keep the file name same as in the basepackages.

 


 

2 Check the resource inAPK

After changed the overlay resources and built thetarget packages, sometimes we need to check if the overlaydirectory took effects and the values are changed in the finallygenerated apks.

2.2 Using 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.

 

Forexample:

1. To dumpstring, bool values:

aapt dump resources Settings.apk

 

2. To dumpa raw xml file:

aapt dump xmltree Settings.apk res/xml/appwidget_info.xml

 

3. To dumpthe current configurations/localizations:

aapt dump configurations Settings.apk

 

2.2 Usingapktools

 

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.apktool
For smali/baksmali info, see: http://code.google.com/p/smali/

 

2.3 Usingdumpres

 


 

3 More on AAPT andOverlay

3.1 How overlayworks

Whilebuilding the package APKs, the overlay directories are passed toaapt command lines using -S options in the same order as they are definedin 

PRODUCT_PACKAGE_OVERLAYS and DEVICE_PACKAGE_OVERLAYS.

Forexample, while building the Settings APK, the following command areexecuted:

 

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 theSettings resources will be filtered first, and do not appear in theabove command line.

 

3.2 Add extra resources inOverlay

Though not recommanded, we can add new resources in overlaydirectory, for example, if base package Settings doesn't define a bool value with key no_such_key, wecan add it in the overlay file bool.xml likethis:

    ... ...
    
    true
    ... ...

 

Ifthe add-resource lineis missing, aapt toolwill 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 toadd.

 

Another way to avoid the complaint is torun aapt with the option:

 

--auto-add-overlay
    Automatically add resources that are only in overlays.


分享:

喜欢

0

赠金笔

阅读 评论 收藏 转载 喜欢 打印 举报
已投稿到:
排行榜
前一篇: Linux 数据对齐
后一篇: Android点滴积累系列--framework/base新增文件统一管理

最后

以上就是飘逸盼望为你收集整理的Android点滴积累系列--资源overlay机制(转)的全部内容,希望文章能够帮你解决Android点滴积累系列--资源overlay机制(转)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部