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
- SVG: The root
<svg>tag defines the dimensions viawidth,height, and the internal coordinate space viaviewBox="minX minY width height". - VectorDrawable: The root becomes
<vector>.widthandheightmap toandroid:widthandandroid:height(intrinsic display size).viewBoxmaps toandroid:viewportWidthandandroid:viewportHeight(the virtual coordinate grid for drawing).
2. Paths and Shapes
- SVG: SVG supports standard
<path d="...">as well as primitive shapes like<rect>,<circle>,<ellipse>,<line>, and<polygon>. - VectorDrawable: All primitive shapes are
mathematically converted into standard path commands. The result is
always a
<path>element with the path geometry stored in theandroid:pathDataattribute.
3. Groups and Transformations
- SVG: The
<g>tag groups elements and applies transformations such astransform="rotate(...)"ortransform="scale(...)". - VectorDrawable: Maps directly to
<group>, translating matrix transforms into explicit attributes such asandroid:rotation,android:pivotX,android:pivotY,android:scaleX,android:scaleY,android:translateX, andandroid:translateY.
4. Colors and Styling
- SVG: Attributes like
fill,stroke,stroke-width,stroke-linecap, andstroke-linejoindefine element rendering. - VectorDrawable: These convert to:
android:fillColorandroid:strokeColorandroid:strokeWidthandroid:strokeLineCapandroid:strokeLineJoin
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:
- Text Elements (
<text>): Android cannot render raw SVG text nodes dynamically within a vector asset. The converter requires text elements to be converted to outlines (paths) prior to or during conversion. - Complex Gradients: Linear and radial gradients are
supported starting from Android API 24 using child
<gradient>tags inside<path>, but complex multi-stop mesh gradients are flattened or discarded. - Clip Paths: SVG
<clipPath>nodes are converted to<clip-path android:pathData="..."/>within a<group>, restricting the rendering boundary for subsequent paths inside that group. - Filters and Blurs: Elements such as
<filter>,<feGaussianBlur>, or drop shadows are stripped entirely.
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.