这是这个问题的后续问题:
将 Android 支持库更新到 23.2.0 会导致错误:XmlPullParserException 二进制 XML 文件行 #17
我还将支持库更新到23.2并开始收到错误:
XmlPullParserException Binary XML file line #17<vector> tag requires viewportWidth > 0
这个问题解决了 Android Studio 和 Gradle 的问题。使用没有 Gradle 的 Eclipse 时如何解决这个问题?
您可以切换回 appcompat 库的先前版本(快速修复):
compile 'com.android.support:appcompat-v7:23.1.1'
或者保留当前的库版本并对构建 gradle 文件进行适当的更新,如 google 在版本 23.2.0 发行说明中所解释的那样。
//for Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
如果您使用 Gradle 1.5,您将使用
defaultConfig {
generatedDensities = []
}
// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}
不要忘记将你的 gradle 构建工具至少更新到 1.5.0 版本,否则你将无法使用新参数,如 generatedDensities:
classpath 'com.android.tools.build:gradle:1.5.0'
有关原因的更多信息这里
这个问题的先前答案为使用 Gradle 的开发人员提供了一个解决方案,但我不使用 Gradle,所以我想总结一下他的答案,它帮助了几个人以及我最终做了什么。我接受了我自己的答案,而不是他的答案,因为就像我说的,我不使用 Gradle,所以我没有使用他写的内容。
为了让它最终发挥作用,我做了几件事。可能的解决方案是:
首先对于 Gradle 用户:
1)将支持库恢复到旧版本,因为这个版本有一个错误。
2)使用编译'com.android.support:appcompat-v7:23.2.1',因为错误已在那里修复。
3) 对于 Gradle 插件 2.0:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
或者您可以使用 Grade Build Tools 1.5.0 (
classpath 'com.android.tools.build:gradle:1.5.0'
)
defaultConfig {
generatedDensities = []
}
// Gradle 2.0 不需要这样做
aaptOptions {
additionalParameters "--no-version-vectors"
}
这是非 Gradle 用户的部分:
1) 打开SDK管理器。
2) 从 API 22 和 23 中卸载了“Android Wear X”(其中 X 是 ARM 或 Intel)。
3)然后我在 AppCompat 库的其中一种样式中仍然遇到编译错误。我只是将它们注释掉(如果使用非常特定的样式,我将冒非常特定的设备无法工作的风险)。
之后我清理了项目,它就开始工作了。
仅适用于新用户,此问题已修复:
compile 'com.android.support:appcompat-v7:23.2.1'
尝试已提供的答案后,该应用程序在某些设备(主要是三星)上崩溃。除此之外,我尝试加载像这样的矢量绘图
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, R.drawable.resource_id);
这个
AppCompatDrawableManager
在内部尝试使用各种方法获取可绘制对象:
Drawable getDrawable(@NonNull Context context, @DrawableRes int resId,
boolean failIfNotKnown) {
checkVectorDrawableSetup(context);
Drawable drawable = loadDrawableFromDelegates(context, resId);
if (drawable == null) {
drawable = createDrawableIfNeeded(context, resId);
}
if (drawable == null) {
drawable = ContextCompat.getDrawable(context, resId);
}
if (drawable != null) {
// Tint it if needed
drawable = tintDrawable(context, resId, failIfNotKnown, drawable);
}
if (drawable != null) {
// See if we need to 'fix' the drawable
DrawableUtils.fixDrawable(drawable);
}
return drawable;
}
因此它适用于所有 Android 版本和所有设备(希望如此)。
注意:不要尝试使用Picasso(2.5.2)或Glide(3.7.0)的方法来加载矢量绘图。因为他们内部使用已弃用的
getDrawable(@DrawableRes int id)
方法。在某些设备上这会导致 Resources.NotFoundException
。
在将 Gradle 依赖项更新到最新版本但忘记更新项目的 buildToolsVersion 后,我在 Android Studio 2.2 中遇到了同样的错误。
我改变了:
compile 'com.android.support:appcompat-v7:22.2.1'
至:
compile 'com.android.support:appcompat-v7:24.2.1'
虽然 buildToolsVersion 保持在“22.0.1”,如下所示:
buildToolsVersion "22.0.1"
所以我所做的就是将 buildToolsVersion 更新为
24
,如下所示:
buildToolsVersion "24"
因为它之前已通过 SDK Manager 下载。因此,请从 SDK Manager 检查最新的
buildToolsVersion
,看看它是否与依赖项版本匹配。
希望这对某人有帮助。
对于像我这样仍在不使用 Gradle 的情况下使用 Eclipse 的人来说,我在 Lollipop 之前的设备上运行 Android 支持库 r23.2.0 时遇到了此错误(API < 21).
此问题已在 r23.2.1 中修复,我已经能够在 API 级别 16 上成功运行我的项目。
此版本的 Eclipse 库不再通过 SDK Manager 提供,但是您可以从此链接手动下载它。
如果问题出现在发布的应用程序版本中。这解决了我的 发布。我想是因为如果你把
shrinkResources true
创建的可绘制文件夹没有可绘制文件。
{
..
minifyEnabled true
shrinkResources false
}
我在将一些 PNG 文件转换为 SVG 时遇到了此错误。 事实证明,问题是我正在加载 XML 状态列表。在 Android API 上 <= 23 the parser expects a viewportWidth/Height but a statelist doesn't have that.
因此,为了在这些较旧的 Android 上修复它,我(除了启用 VectorCompat 功能之外)是使用硬编码的状态列表。由于我只需要 1 个状态列表,所以不需要太多努力,现在我的 SVG 状态列表可以在 Android 21 上正常工作。
private Drawable createVectorButtonWithTheme(int theme) {
if (Build.VERSION.SDK_INT <= 23) { //vector compat doesn't work with xml statelists somehow on 6.0 and lower
return createVectorStateListWithTheme(theme);
} else {
ContextThemeWrapper themeWrapper = new ContextThemeWrapper(this, theme);
Drawable vector = VectorDrawableCompat.create(getResources(), R.drawable.button, themeWrapper.getTheme());
return vector;
}
}
private StateListDrawable createVectorStateListWithTheme(int theme) {
ContextThemeWrapper themeWrapper = new ContextThemeWrapper(this, theme);
Drawable button = VectorDrawableCompat.create(getResources(), R.drawable.button_vector, themeWrapper.getTheme());
Drawable button_pressed = VectorDrawableCompat.create(getResources(), R.drawable.button_vector_pressed, themeWrapper.getTheme());
Drawable button_disabled = VectorDrawableCompat.create(getResources(), R.drawable.button_vector_disabled, themeWrapper.getTheme());
StateListDrawable res = new StateListDrawable();
res.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, button_pressed);
res.addState(new int[]{android.R.attr.state_enabled}, button);
res.addState(new int[]{}, button_disabled);
return res;
}