14

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 ?

arober11
  • 1,969
  • 18
  • 31
Rob Anderson
  • 2,377
  • 3
  • 22
  • 31
  • 1
    While SVGs as the basis for drawable resources was announced at Google I|O 2015 yesterday, AFAIK it requires Android Studio 1.3. There are third-party Gradle plugins that offer conversion of SVGs to drawables. Beyond that, I am not aware that SVGs were ever legal drawable resource files. – CommonsWare May 29 '15 at 16:15
  • Thanks, but I tried it with Android Studio 1.3+ but it did not help. I of course clean the project a few times, nothing. I never had problems with using SVGs even in the early day of Android Studio. – Rob Anderson May 29 '15 at 17:33

6 Answers6

21

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

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
  • Thanks, after a year of using an outdated Gradle, I can now move to the most recent one :) – velis Apr 12 '16 at 14:27
  • How to add this property,In my project there is no file name called "gradle.properties"? – Srinivasan Apr 20 '16 at 10:19
  • @Srinivasan [create the file](https://docs.gradle.org/current/userguide/build_environment.html)?! ;) – TWiStErRob Apr 20 '16 at 10:25
  • @TWiStErRob great solution. It helps me to use Gradle 2.10, I have an another problem after updating http://stackoverflow.com/questions/36787855/aapt-err-libpng-error-not-a-png-file – Srinivasan Apr 22 '16 at 07:48
7

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'
  }
}
afrank
  • 94
  • 5
6

Simple Import via "Resource manager" svg to xml

Fortran
  • 2,218
  • 2
  • 27
  • 33
1

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.

Peter
  • 10,910
  • 3
  • 35
  • 68
  • What if I want to automatically convert SVGs to PNG drawables?? Isn't that supposed to be possible in the new Android Studio? – IgorGanapolsky Jun 09 '15 at 18:34
  • This is not yet available (but has been pronounced during the Google I/O 2015). I'm using the solution that I described above and will switch to the final solution once this is available. – Peter Jun 24 '15 at 19:45
  • I don't think *should* is an appropriate suggestion. There are many advantages to using 3rd party SVG libraries, most notably, backward compatibility. – DoctorDbx Aug 16 '15 at 05:03
1

Add the following to your gradle.properties :

android.disableResourceValidation=true

atitpatel
  • 3,104
  • 1
  • 24
  • 34
0

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.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Noam a
  • 199
  • 1
  • 6
  • My guess would be that the system just can't handle SVG files as drawables, but I'm pretty sure it would be happy to package as `raw` or `assets`. – TWiStErRob Mar 06 '16 at 22:38