How Android Converts SVG to VectorDrawable XML

Android does not render standard Scalable Vector Graphics (SVG) files directly at runtime due to the performance overhead of the full SVG specification. Instead, the platform relies on its own proprietary format called VectorDrawable. This article explains the conversion process handled by Android’s build tools and Android Studio, detailing how standard SVG elements, coordinate systems, and style attributes are parsed, simplified, and mapped into optimized VectorDrawable XML assets.

The Role of Vector Asset Studio and Svg2Vector

The primary conversion mechanism in the Android ecosystem is a tool called Svg2Vector, which is integrated into Android Studio’s Vector Asset Studio.

When an SVG file is imported: 1. Document Parsing: The converter parses the XML Document Object Model (DOM) of the SVG file. 2. Feature Filtering: It filters out unsupported or complex SVG features, such as embedded JavaScript, CSS style blocks, text nodes, and heavy raster effects. 3. Geometry Translation: It translates standard SVG elements into Android-compatible vector tags. 4. Output Generation: It outputs a standard Android XML resource placed in the res/drawable/ directory.

Core Tag and Attribute Mapping

The converter establishes a direct mapping between W3C SVG nodes and Android vector tags:

1. The Root Element

2. Paths and Shapes

3. Groups and Transformations

4. Colors and Styling

Path Data Command Normalization

SVG path data uses commands such as M (MoveTo), L (LineTo), C (Cubic Bézier), S (Smooth Cubic Bézier), Q (Quadratic Bézier), A (Elliptical Arc), and Z (ClosePath).

Android’s VectorDrawable natively supports the same standard path commands inside android:pathData. During conversion: * Relative path commands (lowercase letters like m, l, c) and absolute path commands (uppercase letters) are preserved. * Redundant commands and whitespace are minified. * Non-standard path curves or complex compound paths are normalized into standard Bézier curve syntax.

Handling Unsupported SVG Features

Because VectorDrawable is designed specifically for fast rendering on mobile hardware, certain SVG specifications are handled with limitations:

Compilation to Binary Resource

Once the SVG is converted to a VectorDrawable XML file: 1. The Android Asset Packaging Tool (AAPT2) compiles the XML file into a binary XML format during the project build process. 2. At runtime, the Android framework (or the VectorDrawableCompat library for backward compatibility) parses the binary XML into memory. 3. The parsed commands are passed directly to the native Android 2D rendering pipeline (Skia / HWUI), which executes the canvas drawing operations on the GPU.