• Stars
    star
    1,007
  • Rank 45,632 (Top 0.9 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use SVGs as resources in Android

victor

What's our vector, Victor? -Captain Oveur

Use SVGs as resources in Android!

With this plugin, you can define source folders for SVGs and they will automatically be rasterized/included in your build without messing with your source code.

Installation

Add the following to your build.gradle:

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'com.trello:victor:1.1.2'
    }
}

apply plugin: 'com.android.application'

// Make sure to apply this plugin *after* the Android plugin
apply plugin: 'com.trello.victor'

Usage

Victor adds the svg source set to the Android plugin. You can define where your SVG folders are in the same way you define any other source sets:

android {
    sourceSets {
        main {
            svg.srcDir 'src/main/svg'
        }
    }
   sourceSets.all { // this sets for all at once
        svg.srcDir "src/${name}/svg"
    }
}

You can have multiple SVG folders for a variety of build types/product flavors; or you can just use 'main' to cover them all.

Additional configuration can be done in the victor closure:

victor {
    // Any assets defined in relative terms needs a base DPI specified
    svgDpi = 72

    // Do not generate these densities for SVG assets
    excludeDensities = [ 'ldpi', 'xxxhdpi' ]

    // WARNING: EXPERIMENTAL
    // Generates Android drawables instead of PNGs.
    //
    // This is known not to work on only a subset of SVGs (e.g., does not support any value besides px).
    generateVectorDrawables = true
}

Usage (Kotlin)

Here's how to configure the Victors svg source-set extension via Gradle Kotlin DSL, with both an example for an individual source-set and for setting for all source-sets.

val Any.extensions get() = (this as org.gradle.api.plugins.ExtensionAware).extensions
android {
    sourceSets {
        // Example with setting one
        named("main") {
            val svgSourceSet = this.extensions["svg"] as SourceDirectorySet
            svgSourceSet.srcDir("src/${name}/svg")
        }
    }
    // Example setting all
    sourceSets.all {
        val svgSourceSet = this.extensions["svg"] as SourceDirectorySet
        svgSourceSet.srcDir("src/${name}/svg")
    }
}

You can have multiple SVG folders for a variety of build types/product flavors; or you can just use 'main' to cover them all.

Additional configuration can be done in the victor closure:

configure<com.trello.victor.VictorPluginExtension> {
    // See comments in gradle section
    svgDpi = 72
    excludeDensities = [ 'ldpi', 'xxxhdpi' ]
    generateVectorDrawables = true
}

OSX Issues

When using Android Studio on OSX, you might see this error:

Toolkit not found: apple.awt.CToolkit

This occurs because Batik, the SVG toolkit Victor uses, requires a working version of AWT.

If this happens you should install and use JDK 1.7 for your instance of Android Studio. For instructions how, consult the "Mac OS X" section of this article.

Known Issues

  • Android Studio doesn't recognize generated resources in XML, so autocomplete doesn't work and you get warnings (even though the code works fine). Generated resources should be fully supported in future versions of the Android Gradle plugin.

  • Android Studio doesn't automatically rebuild if the SVG folder is modified (like it does with other resources). Therefore, if you add SVGs you will have to manually rebuild before they will be generated.

  • Due to bugs in Batik, the SVG toolkit used by Victor, some SVGs containing 'mask="url(#mask-2)"' references are not rasterized correctly. Sketch and other tools occasionally produce assets with these references.

Planned Features

  • 9-patch support

More Repositories

1

RxLifecycle

Lifecycle handling APIs for Android apps using RxJava
Java
7,720
star
2

navi

Adds listening capabilities to Activities and Fragments
Java
617
star
3

scientist

A Node.js library for carefully refactoring critical paths in production
CoffeeScript
408
star
4

trellisheets

Guidelines, resources, and examples for writing CSS for Trello
HTML
253
star
5

mr-clean

Don't leak sensitive data.
Kotlin
230
star
6

iconathon

An icon task runner that convert Sketch files to mobile and web formats.
CoffeeScript
219
star
7

power-up-template

A static GitHub pages hosted sample Power-Up
JavaScript
123
star
8

trello-ios-assisted-onboarding

This project is a simple iOS App that hosts the Trello iOS Assisted Onboarding screens.
Swift
44
star
9

node-dependencies

Check out-of-date dependencies for your Node.js app
JavaScript
34
star
10

chromello

A sample Chrome extension written for Trello with a few great features.
JavaScript
32
star
11

api-docs

The documentation site for the Build with Trello content
27
star
12

weather-power-up

A small sample Power-Up for Trello that shows weather data on cards
JavaScript
21
star
13

trellicolors

Converts the Trello brand colors to various formats.
CoffeeScript
19
star
14

category-theory

sometimes math is just too much fun
19
star
15

node-coffee-cache

Caches the contents of required CoffeeScript files so that they are not recompiled to help improve startup time
JavaScript
18
star
16

full-name-splitter

Attempts to split a Latinesque fullname into first name and last name components
JavaScript
17
star
17

glitch-trello-power-up

Example Glitch Project Using Many Power-up Capabilities
JavaScript
15
star
18

glitch-power-up-tutorial-part-one

JavaScript
12
star
19

node-coffee-backtrace

Give some context to uncaught exceptions for Node.js projects written in CoffeeScript
JavaScript
7
star
20

diplomat

A Slack bot for making international collaboration and communication more seamless.
JavaScript
6
star
21

support-team-bookmarklets

Some bookmarklets you can run while using Trello
JavaScript
6
star
22

hearsay

A library for observing things
CoffeeScript
6
star
23

staunton

The massive multiplayer Chess game slash ✨ ReactiveCocoa tutorial ✨
Objective-C
6
star
24

magellan

Mapping and routing for REST endpoints
Objective-C
4
star
25

trello.cards

Less
3
star
26

power-up-on-heroku

A simple Trello Power-Up hosted on Heroku.
JavaScript
3
star
27

code-snippets

Trello Code Snippets Power-Up
JavaScript
3
star
28

yeoman-generator-trello

A Yeoman generator for quickly getting started with the Trello API.
JavaScript
2
star
29

url-parse-fix-auth

Fix url.parse to work with percent (%) characters in auth strings
JavaScript
2
star