Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an application's vector drawable override a library's vector drawable?

I have a library project and an application project. Beside other things the libary project contains some png and some vector drawables. Now I can easily overwrite a png drawable in the application project by giving it the same name and it will be displayed correctly. This does not work with vector drawables, though: The app always shows the vector drawables defined in the library, both on Android 4 and 5. The app would never show the application project's vector drawables.

Google claims an application's resources always have priority over a library's resources:

Since the tools merge the resources of a library module with those of a dependent application module, a given resource ID might be defined in both modules. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

But as I said, in case of vector drawables, for some reason, it's the other way round. Any idea what I can do to make sure the vector drawables are overridden just like normal drawables and other resources are?

UPDATE: Resolved in support library v23.2! Nothing to do now :)

like image 392
0101100101 Avatar asked Jan 05 '16 09:01

0101100101


People also ask

What is VectorDrawable?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.

What is the default location of the vector icons imported through Android studio?

Vector Asset Studio adds an XML file defining the vector drawable to the project in the app/src/main/res/drawable/ folder. From the Android view of the Project window, you can view the generated vector XML file in the drawable folder.


2 Answers

When the library project is built, the VectorDrawable creates PNG files for each density and places the original VectorDrawable in drawable-anydpi-v21.

If you place your VectorDrawable in drawable-anydpi-v21 in your app, then it will override the drawable from your library project. This seems like a bug and a new issue should be created (if one doesn't already exist).


NOTE: this will not replace the generated PNG files from the library. You will need to add those to your app as well to override them.

like image 85
Jared Rummler Avatar answered Sep 20 '22 12:09

Jared Rummler


Based on the "NOTE" in Jared Rummler's answer and since this won't fit in a comment I'll post a little tutorial here for people who have trouble finding the generated PNG files:

You can find the folders with the required files inside build/intermediates/res/merged/debug or, if you are using product flavors build/intermediates/res/merged/<flavor>/debug. Now the least difficult way of copying the PNG files to the app would be to fully copy their folders, which are:

drawable-ldpi-v4
drawable-mdpi-v4
drawable-hdpi-v4
drawable-xhdpi-v4
drawable-xxhdpi-v4
drawable-xxxhdpi-v4

As a last and tedious step you should remove all files inside you don't need, i.e. those that aren't generated from your vector drawables. This is done easiest if you are using a VCS by adding only the PNGs you need. This places all redundant files under Unversioned Files.

And there you go, together with drawable-anydpi-v21 there are now 7 additional folders just because of this stupid bug :(

UPDATE: Resolved in support library v23.2! As of today, there is finally no need to do any of the above. Just make sure to use app:srcCompat instead of android:src everywhere.

like image 27
0101100101 Avatar answered Sep 20 '22 12:09

0101100101