Redirecting to https://developer.android.com/studio/build/shrink-code.html
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 aapt which acted as overlays, alongside the --auto-add-overlay to 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 aapt and 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 priority order is the following:
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/res has
and src/debug/res has
Then the merged resource folder will contain the default foo.xml from src/debug/res but the landscape version from src/main/res
As mentioned above, each sourceSet can define multiple resource folders. For instance:
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.
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:
The flat list passed to the merger will be A, D, B, C
This ensures that both A and B overrides C.