I have an Android application project that depends on two Android libraries. The two Android libraries specify two resources with the same resource ID. In the old Ant build system, the priority of libraries was specified in a project.properties file, but such a file is not used in the Gradle build system.
Although the Resource Merging doc explains the priorities and merge process for resources that conflict between build types, product flavors, application projects, and library projects, it does not explain what occurs when two libraries (who have no common dependencies) are merged in a single project. During the merge process of the build process, how does the Android Gradle plugin determine which library's resource has a higher priority?
There is not a way to specify a priority for the library's resources.
You can only setup the prefix in your library with
android {
resourcePrefix 'mylib_'
}
Ah, looks like the Android developers documentation finally has an answer for us. I pulled this from https://developer.android.com/studio/projects/android-library#Considerations:
The build tools merge resources from a library module with those of a dependent app module. If a given resource ID is defined in both modules, the resource from the app is used.
If conflicts occur between multiple AAR libraries, then the resource from the library listed first in the dependencies list (toward the top of the dependencies block) is used.
To avoid resource conflicts for common resource IDs, consider using a prefix or other consistent naming scheme that is unique to the module (or is unique across all project modules).
I bolded the relevant paragraph. It looks like that the order in which dependencies can appear in a Gradle dependencies block can affect the build process! This is a small thing to be wary of.
@Angela answer is great for solving your (and actually my) problem, for answering your problem according to how Android choose the right resource and what exactly the priority:
From the resource merging part in the build process:
The priority order is the following:
BuildType -> Flavor -> main -> Dependencies.
...
This means that if src/main/res
has
res/layout/foo.xml
res/layout-land/foo.xml
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