简体   繁体   English

Android:在res中使用SVG会导致错误:“文件名必须以.xml或.png结尾”

[英]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. 我刚刚更新了Android Studio 1.2.1.1,现在gradle在我的资源文件夹中遇到svg问题。 I was always using SVGs with no problem and I hope they will be "allowed" in the future. 我总是使用SVG没有问题,我希望它们将来会被“允许”。

: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 ? 任何想法如何解决?

SVG files are not supported by Android as drawables. Android不支持SVG文件作为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). 而是尝试将它们放在src/main/res/rawsrc/main/assets文件夹中并适当加载它们(通过外部库)。 Most people confuse SVG and Vector-drawables, they're not the same thing. 大多数人混淆SVG和Vector-drawables,它们不是一回事。 Vector-drawables use SVG-compatible path mini-language, but that's about it. Vector-drawables使用与SVG兼容的路径迷你语言,但就是这样。

The current accepted solution is to revert back to an archaic version of the Gradle plugin, which is counterproductive in most cases. 当前接受的解决方案是恢复到Gradle插件的古老版本,这在大多数情况下会适得其反。 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. 你应该向前拉到1.5.0 (现在已经稳定了一段时间)并将android.disableResourceValidation=true添加到你的gradle.properties文件中(通常位于settings.gradle旁边的根项目settings.gradle ),而不是恢复你。 -1.2.2行为。

For more info see http://b.android.com/192493 有关详细信息,请参阅http://b.android.com/192493

Same problem with webp files in the drawable resource directory. 可绘制资源目录中的webp文件也存在同样的问题。

The latest gradle plugin (v1.2.3) is not recognizing some of the image formats that used to work before. 最新的gradle插件(v1.2.3)无法识别以前曾经使用过的某些图像格式。 I don't have the issue when I switch back to v1.2.2 当我切换回v1.2.2时,我没有这个问题

UPDATE: Given that this feature was fixed, you should upgrade to the latest gradle plugin release instead of reverting back. 更新:鉴于此功能已得到修复,您应该升级到最新的gradle插件版本而不是恢复。

You can change the version in your build.gradle by setting: 您可以通过设置更改build.gradle中的版本:

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

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 ). 在Android中,您应该使用(XML)Vector Drawable文件(它们包含XML中的SVG信息,详细信息: https//developer.android.com/training/material/drawables.html#VectorDrawables )。

Vector Drawables can be downloaded from this repository: https://github.com/google/material-design-icons . Vector Drawables可以从这个存储库下载: https//github.com/google/material-design-icons Look for the folders called drawable-anydpi-v21 within the icon directories. 在图标目录中查找名为drawable-anydpi-v21的文件夹。 This folder includes the Vector Drawable files to be placed within the drawable directory of your app. 此文件夹包含要放置在应用程序的drawable目录中的Vector Drawable文件。

Note that working with the Vector Drawable files is only supported for Android 5.0 and higher. 请注意,仅支持Android 5.0及更高版本才能使用Vector Drawable文件。 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. 如果您需要与<5.0 Android版本兼容,我建议使用此库: https//github.com/trello/victor此库可用于在编译期间将SVG文件转换为不同格式的drawable你的应用。 Which saves you from having to create and mantain mdpi/hdpi/xhdpi/xxhdpi folders manually. 这使您无需手动创建和保存mdpi / hdpi / xhdpi / xxhdpi文件夹。

In case you name the Vector drawable files and the SVG files equally, you can use both approaches next to each other. 如果您同等地命名Vector drawable文件和SVG文件,则可以使用彼此相邻的两种方法。

Add the following to your gradle.properties : 将以下内容添加到gradle.properties:

android.disableResourceValidation=true android.disableResourceValidation =真

the problem still remains on gradle 1.5. 问题仍然存在于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. 似乎未来唯一的选择是使用“创建矢量资产”将svg文件转换为xml,然后选择svg file选项。

there is a problem in android studio 1.5.1 while using build:gradle:1.5.0.please give the solution. 使用build时,android studio 1.5.1中存在问题:gradle:1.5.0.please给出解决方案。 I just updated Android Studio 1.5.1 and now gradle got problems with svg in my resources folder. 我刚刚更新了Android Studio 1.5.1,现在gradle在我的资源文件夹中遇到svg问题。 I was always using SVGs with no problem and I hope they will be "allowed" in the future. 我总是使用SVG没有问题,我希望它们将来会被“允许”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM