Androidアプリではベクター形式の画像を扱うためにVectorDrawableというSVGと(おおよそ)互換のあるフォーマットをサポートしています。しかし残念なことにVectorDrawableの出力をサポートしているデザインツールはほぼ無いためベクターデータをアプリ内で扱いたい場合には「デザインツールからSVGに書き出してもらう-> Vector Asset Studioを利用してSVGをVectorDrawableに変換する」という手順を踏みます。
Vector Asset StudioはAndroid Studioに組み込まれたツールでGUIインターフェースしかサポートされていません。これは悩みの種で例えば複数のプラットフォーム間で共通利用しているアイコンセットがあった場合にはSVGが追加/更新されたらVectorDrawableも追従するCI/CDを用意したいものですがCLIからアクセスする手段が無いので自動化出来ないんですよね。そんなことをTwitterに書いたらこにふぁーさんから素晴らしい情報を教えてもらったので今文字を書いています。
だいぶ前の情報なので今も参考になるかはわかりませんが、以前こういう方法でやっていたことはありますhttps://t.co/Dbs7sjbGKo
— こにふぁー (@konifar) July 31, 2020
教えて頂いたIssueTrackerの要点は
- Vector Asset Studio自体はAOSPの一部として開発されているからソースコードにアクセスすることが出来るよ
- 実は
vd-tool
というCLI用のコマンドが存在していて、自分でビルドすれば使えるよ - ただ今の所公式に提供するつもりはないよ
という話でした。2016年に立てられたissueの情報のため今のレポジトリでは多少手順が変わっているのかなと思ってコードをチェックアウトして試してみたのですが、全く同じ手順でビルド出来ました。:tada:
vd-toolをビルドする
まずはAOSPからAndroidStudioが開発されているブランチをチェックアウトします。repo
コマンドの導入からまとめると下記のようなになります。(パスはお好みで)
# repoコマンドを使えるようにする curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo chmod a+x ~/bin/repo # studio-master-dev(Android Studioの開発ブランチ)をチェックアウトする mkdir studio-master-dev cd studio-master-dev repo init -u https://android.googlesource.com/platform/manifest -b studio-master-dev repo sync -c -j4 -q #4は並列数なのでお好みで
Android Developer Tools - Checkout and build the source code
チェックアウトしたらvd-toolをビルドします
cd tools/
./gradlew :base:vector-drawable-tool:distZip
SVGを一括で変換する
ビルドが成功すると $PROJECT_ROOT/out/build/base/vector-drawable-tool/build/distributions
にvd-tool.zip
が生成されているので解凍して利用します。
例えば下記のように実行すると/your/icon/svg/
以下のsvgをすべてVectorDrawableに変換して/your/icon/vd
に配置してくれます。
bin/vd-tool -c -in /your/icon/svg -out /your/icon/vd
引数でwidthやheightの指定も可能です
Usage: [-c] [-d] [-in <file or directory>] [-out <directory>] [-widthDp <size>] [-heightDp <size>] [-addHeader] Options: -in <file or directory>: If -c is specified, Converts the given .svg file to VectorDrawable XML, or if a directory is specified, all .svg files in the given directory. Otherwise, if -d is specified, displays the given VectorDrawable XML file or all VectorDrawables in the given directory. -out <directory> If specified, write converted files out to the given directory, which must exist. If not specified the converted files will be written to the directory containing the input files. -c If present, SVG files are converted to VectorDrawable XML and written out. -d Displays the given VectorDrawable(s), or if -c is specified the results of the conversion. -widthDp <size> Force the width to be <size> dp, <size> must be integer -heightDp <size> Force the height to be <size> dp, <size> must be integer -addHeader Add AOSP header to the top of the generated XML file