29

After upgrading to 3.0 Android Studio has started to complain about long paths in vector drawables.

The warning says:

Very long vector path (7958 characters), which is bad for performance. Considering reducing precision, removing minor details or rasterizing vector. less... (⌘F1) Using long vector paths is bad for performance. There are several ways to make the pathData shorter: * Using less precision * Removing some minor details * Using the Android Studio vector conversion tool * Rasterizing the image (converting to PNG)

The documentation https://developer.android.com/guide/topics/graphics/vector-drawable-resources.html shows us how to use vector drawables in our apps, and recommends it over png's etc.

I have both been using the Android Studio Vector Conversion Tool and this excellent service for converting SVG's to vector drawables: http://inloop.github.io/svg2android/

Are there other services that does more to reduce vector drawable complexity? Where can I find guidelines on how 'advanced' my vector drawables can be?

Ove Stoerholt
  • 3,843
  • 4
  • 25
  • 33
  • 1
    I literally just released a vector drawable optimization tool called [`avdo`](https://github.com/alexjlockwood/avdo) about an hour ago. You might find it useful! – Alex Lockwood Nov 29 '17 at 22:14

5 Answers5

31

Try optimizing the vector drawable using avocado! It should help reduce the complexity of your paths.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 6
    It reduced the size by 10.5% but unfortunately some of the paths are still too long according to lint. Is there a way to split up the paths or something? Or should I use png? One of the paths is 1595 characters. – Michael Vescovo Feb 12 '18 at 03:03
  • Its redusing size of the files,no change in paths. – RajaReddy PolamReddy Aug 20 '20 at 10:37
  • Read this [article](https://jhavishal10.medium.com/optimize-svgs-remove-lint-warning-of-the-long-vector-path-fe0b34f80782) once – VishalJha Dec 06 '20 at 21:37
12

My path was around 1800 and i used svg-path-editor to lower to around 1000. Still showed the warning but quite close to the max 800

I am sure there is a better way to use this tool but i just copied the pathData from android studio xml to the Path box, pressed Round and got the result back to the xml

If your path is more than 2000, consider converting to png (with mdpi, hdpi, xhdpi...)

enter image description here

epic
  • 1,333
  • 1
  • 13
  • 27
8

There's an extra step you can add before using svg2android, which is running it through svgo

An example pipeline that I use looks like (note that instead of the web tool, I'm using svg2vectordrawable)

~$ svgo image.svg --config=config -o image.svg.optimized
~$ s2v "image.svg.optimized image.xml

My config file looks like (you can play around with it to match your needs):

"plugins": [
    {
        "convertPathData": {
            "leadingZero": false,
            "floatPrecision": 2
         }
    }
]
marianosimone
  • 3,366
  • 26
  • 32
7

Try to use this tool by decreasing Precision.

Levon Petrosyan
  • 8,815
  • 8
  • 54
  • 65
  • 3
    This tool did not work for me. It reduced the file size by 85%. However, it resulted in "Error inflating class ImageView". When I unchecked "Remove Hidden Elements", it prevented the inflation error, but the file was only reduced by 20% which was not enough to avoid the lint warning. – Elletlar Feb 19 '20 at 11:26
  • It worked for me, but. the quality of resulting image was not good enough – Malachiasz May 07 '21 at 14:05
2

This may not be an ideal solution, but Initially I had drawn the vector asset in a single shot as a single object, this resulted in longer <path> problem. I started from scratch again and this time I split my object into many parts i.e. I drew each part of my single object individually and later I grouped them together (I used Inkscape). As a result, when .svg converts to .xml the single <path> tag gets split into multiple shorter <path> tags.

Shiroyasha
  • 21
  • 1