trello-archive / victor

Use SVGs as resources in Android
Apache License 2.0
1.01k stars 49 forks source link

Generate Android vectors for v21 and above #27

Closed romainpiel closed 8 years ago

romainpiel commented 9 years ago

Gradle android build tools 1.4.0-beta1 is bringing support for vectors to png conversion at compile time. We tried it and got back to Victor as it's working better for us. One minor feature requirement I would have though: the build tools are generating pngs only for versions older than v21. For a given vector file (ic_search.svg), the output generated folder looks like this:

drawable-hdpi
    ic_search.png
drawable-xhdpi
    ic_search.png
drawable-v21
    ic_search.xml
... etc...

ic_search.xml being an Android vector file. I don't see why Victor could not do that, anyone has tried to add this feature?

dlew commented 9 years ago

We could probably add that. It'd just be a matter of finding an svg -> Android converter. Do you of any good ones?

romainpiel commented 9 years ago

I only know that one https://github.com/inloop/svg2android but it's written in javascript. I don't know any based on gradle. Sounds like a good idea for a plugin. Actually the gradle build tools thing takes android vectors as an input instead of SVGs so the distinction int he generated folder is easier to make for them.

dlew commented 9 years ago

What the Android plugin does is kind of odd at first. It takes the Android vectors and re-outputs them to a bunch of generate folders, one for each density (in addition to rasterizing PNGs for them pre-21).

Maybe we could steal their SVG -> Android vector code once it's open sourced.

romainpiel commented 9 years ago

So the SVG -> Android vector code is part of Android Studio and not the build tools. Does that usually get open sourced too?

romainpiel commented 9 years ago

Actually branch 1.4 is available: http://tools.android.com/build

dlew commented 9 years ago

Repo sync takes forever, but I've found it: https://android.googlesource.com/platform/tools/base/+/master/sdk-common/src/main/java/com/android/ide/common/vectordrawable/

I'll look into how easy it would be to integrate this with Victor when I get some time.

romainpiel commented 9 years ago

Awesome! I can help you out with that

dlew commented 9 years ago

The overall gameplan is:

  1. Add the tools as a submodule (to make updating easier).
  2. Add Svg2Vector to the path.
  3. Add a second task for each density which converts SVG -> Vector for v21.
dlew commented 9 years ago

Alright, I've just finished my first pass at this and things aren't looking good from a few perspectives:

  1. It's a gigantic PITA to get Svg2Vector building because it depends on having the entirety of the build tools source code available. It wasn't impossible, but just a bit tricky.
  2. The Gradle plugin comes with all of the tools source and leaks it in the builds. Thus what we're compiling against is different from what's actually used. My quick-and-dirty solution was to use jarjar to distinguish our Svg2Vector sources from theirs.
  3. Svg2Vector does not support everything that batik supports, so the output may differ. Critically, one of the first things I noticed is that it only supports px sizing (no relative sizes like pt). So we'd have to edit the source to some extent (making this even harder).

You can check out what I've been up to on https://github.com/trello/victor/tree/dlew/svg2vector but it won't work without manually doing jarjar and whatnot.

tl;dr - this will be a fair amount of work.

dlew commented 9 years ago

I’m not entirely convinced this is worth the effort right now. Actual vector drawables don’t make a difference if you have to package the PNGs, too. Unless there's some other advantage I'm missing.

romainpiel commented 9 years ago

Right, it's not a big thing missing but it would be nice to have it especially for animated vectors on API 21 +. For example you have an animation morphing one path to another. You could define both of them as SVGs, and declare and Android animation describing the morphing without having to redefine the vectors in separate Android vectors xml files. Pretty niche, I agree.

Anyway I thought that converting SVGs and Android vectors were pretty similar but they are apparently not. Feel free to close this ticket if you feel like it's not valuable enough.

dlew commented 9 years ago

I'd say that for your particular situation using the Android plugin is probably better anyways - then you start with exactly the Android vectors you need for the animation.

It's a similar problem, but what made Victor possible was all the hard work that was previously put into batik. I think I'm going to leave this open but wait for a better Svg2Android to come along before engaging more.

romainpiel commented 8 years ago

Small update on this, I'm playing with AS SVG to Android vector sources and I managed to extract + make it compile: https://github.com/RomainPiel/gradle-svg2android Maybe we could plug Victor to that.