Resource Merging

The Gradle-base build system uses a new merging mechanism for resources. In previous build system, merging was done by passing a list of resource folders to aaptwhich acted as overlays, alongside the --auto-add-overlayto ensure that new resources in the overlays would be automatically added (default behavior is for overlays is to only override existing resources, not create new ones).
One of the goals of the Gradle-based build system was providing more flexibility, and one frequently asked feature request was the ability to have more than one resources folders. aapt is not able to handle this so the new build system introduces a new merging mechanism that is run ahead of aaptand generates a single, merged, resources folder that is fed to aapt. This merging has the advantage of being incremental, both through Gradle's input/output change detection, and in the way it is implemented (ie it can rerun the merge by only applying the change in a single file).
The merged resources are coming from 3 types of sources:
  • The main resources, associated with themainsourceSet, generally located in src/main/res
  • The variant overlays, coming from the Build Type, and Flavor(s).
  • The Library Project dependencies, which contribute resources through the res entry in their aar bundle.

Priority Order

The priority order is the following:
BuildType -> Flavor -> main -> Dependencies.

This means that if a resource is declared in both the Build Type and in main, the one from Build Type will be selected.
Note that for the scope of the merging, resources of the same (type, name) but different qualifiers are handled separately.
This means that if src/main/reshas
  • res/layout/foo.xml
  • res/layout-land/foo.xml
and src/debug/reshas
  • res/layout/foo.xml
Then the merged resource folder will contain the default foo.xmlfrom src/debug/resbut the landscape version from src/main/res

Handling Multiple Resource Folders

As mentioned above, each sourceSet can define multiple resource folders. For instance:
android.sourceSets { main.res.srcDirs = ['src/main/res', src/main/res2'] }

In this case, both resource folders have the same priority. This means that if a resource is declared in both folders, the merge will fail and an error will be reported.

Priority Order for Library Dependencies

With transitive dependencies, the actual set of Library Projects seen by a project is not a flat list but a graph. However the merging mechanism only handles a flat priority list. If we consider the following example of dependencies:
Project -> A, B (meaning A higher priority than B) A -> C, D B -> C
The flat list passed to the merger will be A, D, B, C
This ensures that both A and B overrides C.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Java操作数据库之jdbc【原生方式】
  2. java数据结构--链表
  3. 读阿里巴巴Java开发手册v1.2.0之工程结构
  4. Ubuntu12 64位 阿里云服务器端配置mysql+
  5. 记一次java内存分析
  6. 【java开发系列】—— struts2简单入门示
  7. 被老板 "宠着" 是好是坏???
  8. Java使用Velocity模板发送HTML格式邮件并
  9. 浅析java中equals与==的用法
  10. Java se之静态代码块、代码块、构造函数