How to beat ‘Use 0.094 instead of .094…’ in vector drawable
I like to use SVG icons in my apps’ UI. I’m sure you like it as well.
Sometime earlier I had noticed strange warnings from Android Studio in my vector drawable files. It said that vector path contains float numbers format that can lead to errors on some devices. The line was big and had a lot of such numbers. I decided to deal with this issue later since everything was working well on my devices and emulators.
I didn’t think about this until I got a report from QA. Android Studio was right — it may lead to crashes on some devices and we found such device (it is Samsung S4 with Android 5.1). We were surprised because the app didn’t crash on old devices with Android 4.2. I read about this issue and found that this float numbers format is caused by SVG optimization to reduce the file size. Unfortunately, I didn’t found a way to quickly fix all errors. I really don’t understand why Android Studio can’t fix this. So I had to find a solution.
Our project contains almost 200 vector drawable and at least a quarter of them were “infected” (optimized). To struggle the problem, I wrote a simple function with regular expressions, that takes the content of a file and cures it — fixes all optimized float numbers. I ran this code on all my drawable files and forgot about this issue. I was happy.
Next time, when I added an SVG icon to my project I saw these annoying warnings again. I found my previous solution not really handy as I have to run an external program and feed it with my files. I wanted to have such option inside of Android Studio.
This thought lead me to develop Dr.Vector — my first plugin for Android Studio https://plugins.jetbrains.com/plugin/9937-dr-vector-android
Simple shortcut Ctrl + Alt + D converts a file and makes all errors gone. Plugin adds only one option under Edit menu — Fix Floating Points. It can process several cases of float number declaration. Such .55 -.57 1.98.57.95 numbers will be converted to 0.55 -0.57 1.98 0.57 0.95
There is the plugin’s code published at github: https://github.com/eymar/DrVectorAndroid
I would appreciate any ideas how to improve the plugin. Also, if you try to use it and find a case when it lefts some files unfixed, report me, please!
I hope this can be useful for you :)