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

北战南征 提交于 2019-11-28 21:35:33

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'
  }
}

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

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.

Add the following to your gradle.properties :

android.disableResourceValidation=true

Noam a

the problem still remains on gradle 1.5. It seems that the only option in the future is to convert the svg files to xml using the "create vector asset" and then select svg file option.

there is a problem in android studio 1.5.1 while using build:gradle:1.5.0.please give the solution. I just updated Android Studio 1.5.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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!