noev
noev

Reputation: 980

SVG to android vector drawable

I'm looking for a tool that would translate my svg images to android vector drawable assets. Right now I have to convert my assets one by one by clicking new vector asset -> searching in the folder -> selecting correct svg. Is there a tool that I could use in a script that would translate all my svg's to android vector drawables(xml)?

Upvotes: 1

Views: 2855

Answers (4)

Bernard Ladenthin
Bernard Ladenthin

Reputation: 686

To automate the conversion of SVG images to Android Vector Drawables (XML), you can create a script that uses the core functionality of Android Studio's Svg2Vector.parseSvgToXml method. This method is used internally by the Vector Asset tool in Android Studio.

Here is the Vector Asset tool and the primary method for the conversion: VectorAsset.java

It primarily calls:

String errorMessage = Svg2Vector.parseSvgToXml(file.toPath(), outStream);

Note that the NewVectorAssetStep mainly executes these methods:

String name = FileUtil.getNameWithoutExtension(file).toLowerCase(Locale.getDefault());
return FileResourceNameValidator.getValidResourceFileName(name);

NewVectorAssetStep.java

See the following URLs for the referenced methods:

This example implementation shows the usage

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>SvgToVectorDrawableConverter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>google</id>
            <url>https://maven.google.com</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.android.tools</groupId>
            <artifactId>sdk-common</artifactId>
            <version>31.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>33.1.0-jre</version>
        </dependency>
    </dependencies>
</project>
package org.example;

import com.android.ide.common.vectordrawable.Svg2Vector;
import com.android.ide.common.resources.FileResourceNameValidator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public class SvgToVectorDrawableConverter {

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Please provide the path of the SVG file as an argument.");
            return;
        }

        File svgFile = new File(args[0]);
        if (!svgFile.exists()) {
            System.out.println("The specified file does not exist: " + args[0]);
            return;
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String errorMessage;
        try {
            errorMessage = Svg2Vector.parseSvgToXml(svgFile.toPath(), outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (!errorMessage.isEmpty()) {
            throw new RuntimeException(errorMessage);
        }

        String nameWithoutExtension = getNameWithoutExtension(svgFile);
        String validResourceFileName = FileResourceNameValidator.getValidResourceFileName(nameWithoutExtension);

        String xmlFileContent = outputStream.toString(StandardCharsets.UTF_8);
        File outputFile = new File(svgFile.getParent(), validResourceFileName + ".xml");
        try {
            Files.write(outputFile.toPath(), xmlFileContent.getBytes(StandardCharsets.UTF_8));
            System.out.println("Conversion was successful. Output file: " + outputFile.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String getNameWithoutExtension(File file) {
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) return fileName;
        return fileName.substring(0, dotIndex);
    }
}

Upvotes: 0

Chandu
Chandu

Reputation: 504

Right click on the drawable folder and select ->new vector drawable->from image file ->select the svg and out put path into drawable : This video demonstrates it : https://youtu.be/-nYRhX2LsvQ

Upvotes: 0

Raman Sharma
Raman Sharma

Reputation: 177

Android Studio by default provides tool to convert svg image to vector drawables. Here are the steps: File -> New -> Vector Asset After that, lot of there will be option from where you can select your local svg and it will be converted to vector drawable.

Upvotes: 1

noev
noev

Reputation: 980

Found a tool that does the job SvgToVectorDrawableConverter on github

Upvotes: 3

Related Questions