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. 详解SqlServer数据库中Substring函数的用
  2. SQL Server Management Studio(SSMS)复制数
  3. Windows下SQL Serever 2012彻底卸载删除
  4. SQL Server2012数据库备份和还原的教程
  5. 一条SQL语句查询多个数据库
  6. SQL Server数据库附加失败的解决办法
  7. sql server 2008 压缩备份数据库(20g)
  8. SQL server 2016 安装步骤图文教程
  9. 安装SQL Server 2016出错提示:需要安装ora
  10. SqlServer 查询时日期格式化语句