Automating Inkscape to Export Android Drawables

Developing for Android, you need to provide a lot of copies of each image you use, for mdpi, hdpi, xhdpi, xxhdpi, and even xxxhdpi screens – possibly even more in the future! Even if you create your images as vectors rather than hand-pixelling them, it’s still time-consuming to get Inkscape, a free vector drawing application, to export the 5 or so different bitmap files. Fortunately, Inkscape has some automation features built-in – for example, you can tell it to generate a PNG from an SVG via the command line:

inkscape source.svg -w 32 -h 32 -e dest.png

I’ve created a little Windows batch file that will take all the SVG files in a directory, and then create all the various sizes of PNG from it and put them in the right places. It’s available here on GitHub. It’s Windows-only, and expects a certain directory structure, but it should work if you’re using the default project setup from IntelliJ or Android Studio. If not, you should be able to tweak it.

One thing to note is that Inkscape does take a second or so to do each export, and that time unfortunately adds-up when you’re doing 5 exports per file, on potentially dozens of files. If I get around to creating a Gradle script that only exports the SVGs that have changed, I’ll make sure to post that as well.