VectorDrawableTransformer { public static String merge(@NotNull String drawable1, @NotNull String drawable2) { KXmlParser parser = new KXmlParser(); try { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(CharSequences.getReader(drawable1, true)); int token; while ((token = parser.nextToken()) != XmlPullParser.END_DOCUMENT && token != XmlPullParser.START_TAG) { } if (parser.getEventType() != XmlPullParser.START_TAG || !"vector".equals(parser.getName()) || parser.getPrefix() != null) { return drawable1; } StringBuilder result = new StringBuilder(drawable1.length() + drawable2.length()); Indenter indenter = new Indenter(drawable1); int startLine = 1; int startColumn = 1; while ((token = parser.nextToken()) != XmlPullParser.END_DOCUMENT && token != XmlPullParser.END_TAG || parser.getDepth() > 1) { int endLineNumber = parser.getLineNumber(); int endColumnNumber = parser.getColumnNumber(); indenter.copy(startLine, startColumn, endLineNumber, endColumnNumber, "", result); startLine = endLineNumber; startColumn = endColumnNumber; } parser.setInput(CharSequences.getReader(drawable2, true)); while ((token = parser.nextToken()) != XmlPullParser.END_DOCUMENT && token != XmlPullParser.START_TAG) { } if (parser.getEventType() != XmlPullParser.START_TAG || !"vector".equals(parser.getName()) || parser.getPrefix() != null) { return drawable1; } startLine = parser.getLineNumber(); startColumn = parser.getColumnNumber(); indenter = new Indenter(drawable2); while (parser.nextToken() != XmlPullParser.END_DOCUMENT) { int endLineNumber = parser.getLineNumber(); int endColumnNumber = parser.getColumnNumber(); indenter.copy(startLine, startColumn, endLineNumber, endColumnNumber, "", result); startLine = endLineNumber; startColumn = endColumnNumber; } return result.toString(); } catch (XmlPullParserException | IOException e) { return drawable1; } } private  VectorDrawableTransformer(); @NotNull static String transform(@NotNull String originalDrawable, @NotNull Dimension targetSize); @NotNull static String transform(@NotNull String originalDrawable,
                                 @NotNull Dimension targetSize,
                                 @NotNull Gravity gravity,
                                 double scaleFactor,
                                 @Nullable Rectangle2D clipRectangle,
                                 @Nullable Point2D shift,
                                 @Nullable Color tint,
                                 double opacity); static String merge(@NotNull String drawable1, @NotNull String drawable2); @Nullable static Point2D getViewportSize(@NotNull String drawable); @Nullable static Dimension getSizeDp(@NotNull String drawable);  }
@Test public void testMerge() throws Exception { String drawable1 = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<vector xmlns:android=\"http: " android:width=\"94dp\"\n" + " android:height=\"337.5dp\"\n" + " android:viewportWidth=\"94\"\n" + " android:viewportHeight=\"337.5\">\n" + " <group android:translateY=\"133.59375\">\n" + " <path android:pathData=\"M68.41406,-26.71875L25.523438,-26.71875L15.890625,0L1.96875,0L41.0625,-102.375L52.875,-102.375L92.03906,0L78.1875,0ZM29.601562,-37.828125L64.40625,-37.828125L46.96875,-85.71094Z\"\n" + " android:fillColor=\"#000000\"/>\n" + " </group>\n" + "</vector>\n"; String drawable2 = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<vector xmlns:android=\"http: " android:width=\"94dp\"\n" + " android:height=\"337.5dp\"\n" + " android:viewportWidth=\"94\"\n" + " android:viewportHeight=\"337.5\">\n" + " <group android:translateY=\"302.34375\">\n" + " <path android:pathData=\"M68.41406,-26.71875L25.523438,-26.71875L15.890625,0L1.96875,0L41.0625,-102.375L52.875,-102.375L92.03906,0L78.1875,0ZM29.601562,-37.828125L64.40625,-37.828125L46.96875,-85.71094Z\"\n" + " android:fillColor=\"#000000\"/>\n" + " </group>\n" + "</vector>\n"; String merged = "" + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<vector xmlns:android=\"http: " android:width=\"94dp\"\n" + " android:height=\"337.5dp\"\n" + " android:viewportWidth=\"94\"\n" + " android:viewportHeight=\"337.5\">\n" + " <group android:translateY=\"133.59375\">\n" + " <path android:pathData=\"M68.41406,-26.71875L25.523438,-26.71875L15.890625,0L1.96875,0L41.0625,-102.375L52.875,-102.375L92.03906,0L78.1875,0ZM29.601562,-37.828125L64.40625,-37.828125L46.96875,-85.71094Z\"\n" + " android:fillColor=\"#000000\"/>\n" + " </group>\n" + "\n" + " <group android:translateY=\"302.34375\">\n" + " <path android:pathData=\"M68.41406,-26.71875L25.523438,-26.71875L15.890625,0L1.96875,0L41.0625,-102.375L52.875,-102.375L92.03906,0L78.1875,0ZM29.601562,-37.828125L64.40625,-37.828125L46.96875,-85.71094Z\"\n" + " android:fillColor=\"#000000\"/>\n" + " </group>\n" + "</vector>\n"; String result = VectorDrawableTransformer.merge(drawable1, drawable2); assertEquals(merged, result); }