Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Using SVG in res leads to error: "The file name must end with .xml or .png"

I just updated Android Studio 1.2.1.1 and now gradle got problems with svg in my resources folder. I was always using SVGs with no problem and I hope they will be "allowed" in the future.

:app:mergeDebugResources
:app:mergeDebugResources FAILED
/home/petergriffin/folder1/another-app/MyAwesome-App/app/src/main/res/drawable-hdpi/logo.svg
Error:Error: The file name must end with .xml or .png
Error:Execution failed for task ':app:mergeDebugResources'.
> /home/petergriffin/folder1/another-app/MyAwesome-App/app/src/main/res/drawable-hdpi/logo.svg: Error: The file name must end with .xml or .png
Information:BUILD FAILED

Any ideas how to solve that ?

like image 344
Rob Anderson Avatar asked May 29 '15 15:05

Rob Anderson


People also ask

Can Android read SVG files?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.

Where do SVG files go Android studio?

You can import an SVG file as a VectorDrawable in Android Studio, follow these steps : "Right-click" on the res folder and select new > Vector Asset. Select the Local File option and browse to your . svg file.


5 Answers

SVG files are not supported by Android as drawables. Even if you put them there, the system cannot decode them as you would expect. Instead try to put them in src/main/res/raw or src/main/assets folder and load them appropriately (via an external library). Most people confuse SVG and Vector-drawables, they're not the same thing. Vector-drawables use SVG-compatible path mini-language, but that's about it.

The current accepted solution is to revert back to an archaic version of the Gradle plugin, which is counterproductive in most cases. The tools team is investing a lot of effort in making the build much better (it's really noticable).

Instead of reverting you should pull forward to 1.5.0 (has been stable for a while now) and add android.disableResourceValidation=true into your gradle.properties file (usually located in the root project next to settings.gradle) to enable the pre-1.2.2 behavior.

For more info see http://b.android.com/192493

like image 143
TWiStErRob Avatar answered Oct 06 '22 01:10

TWiStErRob


Same problem with webp files in the drawable resource directory.

The latest gradle plugin (v1.2.3) is not recognizing some of the image formats that used to work before. I don't have the issue when I switch back to v1.2.2

UPDATE: Given that this feature was fixed, you should upgrade to the latest gradle plugin release instead of reverting back.

You can change the version in your build.gradle by setting:

buildscript {

  repositories {
      jcenter()
      mavenCentral()
      // mavenLocal() // Only if you want to use your local maven repo
  }

  dependencies {
      // classpath 'com.android.tools.build:gradle:+'
      classpath 'com.android.tools.build:gradle:1.2.2'
  }
}
like image 28
afrank Avatar answered Oct 06 '22 03:10

afrank


Within Android you should be using (XML) Vector Drawable files (they contain the SVG information inside the XML, details: https://developer.android.com/training/material/drawables.html#VectorDrawables).

Vector Drawables can be downloaded from this repository: https://github.com/google/material-design-icons. Look for the folders called drawable-anydpi-v21 within the icon directories. This folder includes the Vector Drawable files to be placed within the drawable directory of your app.

Note that working with the Vector Drawable files is only supported for Android 5.0 and higher. In case you need to be compatible with <5.0 Android version I would recommend to make use of this library: https://github.com/trello/victor This library can be used to convert SVG files to drawables in different formats during compilation of your app. Which saves you from having to create and mantain mdpi/hdpi/xhdpi/xxhdpi folders manually.

In case you name the Vector drawable files and the SVG files equally, you can use both approaches next to each other.

like image 25
Peter Avatar answered Oct 06 '22 01:10

Peter


Add the following to your gradle.properties :

android.disableResourceValidation=true

like image 32
atitpatel Avatar answered Oct 06 '22 03:10

atitpatel


Simple Import via "Resource manager" svg to xml

like image 43
Fortran Avatar answered Oct 06 '22 02:10

Fortran