Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Android Gradle plugin handle conflicting resources in libraries?

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?

like image 389
Alex W Avatar asked Oct 20 '15 21:10

Alex W


2 Answers

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_'
}
like image 148
Angela Avatar answered Sep 22 '22 11:09

Angela


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.

like image 45
Alex W Avatar answered Sep 20 '22 11:09

Alex W