Automatically solve Android build Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge
Automatically solve Android build Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge
Android Studio(使用 SDK 19、21 或 22)显示 Eclipse ADT(使用 SDK 19)不显示的错误:
Error:9-patch image D:\Workspaces....\res\drawable-hdpi\btn_bg_common_press.9.png malformed.
Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge.
或another error:
Error:Ticks in transparent frame must be black or red.
都在aapt
之内
Error:Error: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android\sdk-Android-Studio\build-tools.1.0\aapt.exe'' finished with non-zero exit value 42
文件示例如上,但有 20 多个这样的文件运行良好。
如何使 Android Studio 或 Gradle 跳过此错误并且不必逐个修改这些文件而不会失败?
如果 Gradle 无法实现,我可以使用什么命令行工具将所有透明像素替换为不透明?
下面是应用程序模块(资源所在的位置)的 build.gradle 文件。
我都试过SDK 19 and SDK 21 and build tools 19.1, 21.1.2, 22。
AOSP 上的类似问题,Issue 159464: Android studio: mergeDebugResources FAILED when importing Eclipse project。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.+'
}
}
allprojects {
repositories {
jcenter()
}
}
//---
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':afinal')
compile 'com.android.support:appcompat-v7:19.0.+'
//compile 'com.android.support:appcompat-v7:21.0.+'
}
//---
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
compileSdkVersion 19
buildToolsVersion "19.1.0"
//compileSdkVersion 21
//buildToolsVersion "21.1.2"
//compileSdkVersion Integer.parseInt(project.COMPILE_SDK_VERSION)
//buildToolsVersion project.BUILD_TOOLS_VERSION
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
zipAlignEnabled true
//signingConfig signingConfigs.release
}
debug {
zipAlignEnabled true
}
}
lintOptions {
//checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false // false also required by https://wiki.jenkins-ci.org/display/JENKINS/Android+Lint+Plugin
}
}//android
Android Gradle 插件源位于 https://android.googlesource.com/platform/tools/build/+/master.
Android 现在有两个 PNG 压缩器,一个是 AAPT,另一个是 Java。要忽略构建过程中的 PNG 错误,您可以 force Gradle build to use the old AAPT 通过在 build.gradle 文件中设置以下行:
android.aaptOptions.useAaptPngCruncher = true
这仍然可能给您同样的错误,因为来自最新 SDK 的 AAPT 工具也可能正在检查此错误。所以你有两个选择:
找一个旧的AAPT,例如/build-tools/17.0.0/aapt,用这个旧的覆盖你当前的SDK AAPT。您的构建问题现在可能已解决。如果这不起作用,请尝试以下选项。
编写一个简单的 Bash 或 Perl 脚本,将其命名为 "aapt" 并将其放入您当前的 SDK build-tools 文件夹中。此脚本将为您的 *9.png 调用旧版本 17 AAPT,并为其他所有内容使用新的 AAPT。有关示例脚本,请参阅 this Stack Overflow answer。
How automatically solve the issue (without checking all images in different resolutions)
不可能。由于您希望 .png 表现得像九个补丁(沿边缘扩展,而不是拉伸整个位图),因此您必须将它们格式化为这样 您必须修改文件.
建议的备选方案
由于形状非常简单,您最好删除此文件的所有变体(节省 space、过程中的时间和麻烦)并创建具有以下内容的 /res/drawable/btn_bg_common_press.xml
可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="16dp"/>
<solid android:color="#ccc"/>
<stroke
android:color="#0c0"
android:width="2dp"/>
</shape>
您可以使用 dimen
和 color
资源而不是硬编码值。此外,您可能希望在 shape
中添加 padding
元素
<shape...>
<padding
android:top="8dp"
android:left="8dp"
android:bottom="8dp"
android:right="8dp"/>
</shape>
and/or 将 shape
元素包裹在 inset
元素中。
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:insetTop="8dp"
android:insetLeft="8dp"
android:insetBottom="8dp"
android:insetRight="8dp">
<shape...>
</inset>
如果您这样做,可绘制对象将具有隐含的填充,并且您在设置小部件样式时不必设置它。由于按钮有多个状态可绘制对象,我建议您将它们全部转换为 XML 以确保线宽和角匹配。
尽管根元素的名称是 <shape>
实际上膨胀为 GradientDrawable
(这意味着您可以用渐变而不是纯色填充它)。查看 GradientDrawable
docs 的所有选项。不要曾经以编程方式使用ShapeDrawable
,它根本不起作用。
正在分析 9 个补丁
考虑以下三张放大图像。
候选人#1 是九补丁。它在每一侧都保留了 1px 用于拉伸和填充规范。这一张在拉伸时将表现为普通位图。如果宽度大于高度,则两侧的边框厚度也将大于高度。边框将按比例缩放。
Candidate #2 也是一个九补丁,实际上是说它会拉伸除 1px 边框之外的所有内容,并且每边都有 3px 的隐含填充。拉伸时它会有一个清晰的 1px 边框。
候选人 #3 不是 九补丁。它的缩放方式与#1 相同。
现在让我们看一下您在 OP 中包含的图片的放大版本:
如您所见,它不是一个九补丁,因此它不会被解释为一个补丁,并且构建工具足够友好地警告您这一点。即使较旧的构建工具更宽容并为您在每一侧添加透明 1px,结果也会表现得像普通位图,这意味着当拉伸时它看起来像样本 A 而不是预期结果样本 B。
这是more reading on nine-patches。这解释了每边额外的 1px 的用途。
给出的例子不是 9 块图像,正如它所说的那样。
如果您不想修改每个资源以将其转换为有效的 9 补丁资源,您可以尝试删除资源名称中的“.9”。这样,行为应该是相同的,并且不会出现构建错误。
要解决此问题,请确保用黑色或红色点填充所有边缘。这解决了我所有 SDK 版本的问题。如果您不想垂直或水平缩放或两者都缩放,只需填充所有边缘。在这种情况下,图像不会缩放。
Without error
With malformed error
Android Studio(使用 SDK 19、21 或 22)显示 Eclipse ADT(使用 SDK 19)不显示的错误:
Error:9-patch image D:\Workspaces....\res\drawable-hdpi\btn_bg_common_press.9.png malformed. Error:Frame pixels must be either solid or transparent (not intermediate alphas). - Found at pixel #4 along top edge.
或another error:
Error:Ticks in transparent frame must be black or red.
都在aapt
Error:Error: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android\sdk-Android-Studio\build-tools.1.0\aapt.exe'' finished with non-zero exit value 42
文件示例如上,但有 20 多个这样的文件运行良好。
如何使 Android Studio 或 Gradle 跳过此错误并且不必逐个修改这些文件而不会失败?
如果 Gradle 无法实现,我可以使用什么命令行工具将所有透明像素替换为不透明?
下面是应用程序模块(资源所在的位置)的 build.gradle 文件。
我都试过SDK 19 and SDK 21 and build tools 19.1, 21.1.2, 22。
AOSP 上的类似问题,Issue 159464: Android studio: mergeDebugResources FAILED when importing Eclipse project。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.+'
}
}
allprojects {
repositories {
jcenter()
}
}
//---
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
apply plugin: 'com.android.application'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':afinal')
compile 'com.android.support:appcompat-v7:19.0.+'
//compile 'com.android.support:appcompat-v7:21.0.+'
}
//---
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
compileSdkVersion 19
buildToolsVersion "19.1.0"
//compileSdkVersion 21
//buildToolsVersion "21.1.2"
//compileSdkVersion Integer.parseInt(project.COMPILE_SDK_VERSION)
//buildToolsVersion project.BUILD_TOOLS_VERSION
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
zipAlignEnabled true
//signingConfig signingConfigs.release
}
debug {
zipAlignEnabled true
}
}
lintOptions {
//checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false // false also required by https://wiki.jenkins-ci.org/display/JENKINS/Android+Lint+Plugin
}
}//android
Android Gradle 插件源位于 https://android.googlesource.com/platform/tools/build/+/master.
Android 现在有两个 PNG 压缩器,一个是 AAPT,另一个是 Java。要忽略构建过程中的 PNG 错误,您可以 force Gradle build to use the old AAPT 通过在 build.gradle 文件中设置以下行:
android.aaptOptions.useAaptPngCruncher = true
这仍然可能给您同样的错误,因为来自最新 SDK 的 AAPT 工具也可能正在检查此错误。所以你有两个选择:
找一个旧的AAPT,例如
/build-tools/17.0.0/aapt,用这个旧的覆盖你当前的SDK AAPT。您的构建问题现在可能已解决。如果这不起作用,请尝试以下选项。 编写一个简单的 Bash 或 Perl 脚本,将其命名为 "aapt" 并将其放入您当前的 SDK build-tools 文件夹中。此脚本将为您的 *9.png 调用旧版本 17 AAPT,并为其他所有内容使用新的 AAPT。有关示例脚本,请参阅 this Stack Overflow answer。
How automatically solve the issue (without checking all images in different resolutions)
不可能。由于您希望 .png 表现得像九个补丁(沿边缘扩展,而不是拉伸整个位图),因此您必须将它们格式化为这样 您必须修改文件.
建议的备选方案
由于形状非常简单,您最好删除此文件的所有变体(节省 space、过程中的时间和麻烦)并创建具有以下内容的 /res/drawable/btn_bg_common_press.xml
可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="16dp"/>
<solid android:color="#ccc"/>
<stroke
android:color="#0c0"
android:width="2dp"/>
</shape>
您可以使用 dimen
和 color
资源而不是硬编码值。此外,您可能希望在 shape
padding
元素
<shape...>
<padding
android:top="8dp"
android:left="8dp"
android:bottom="8dp"
android:right="8dp"/>
</shape>
and/or 将 shape
元素包裹在 inset
元素中。
<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:insetTop="8dp"
android:insetLeft="8dp"
android:insetBottom="8dp"
android:insetRight="8dp">
<shape...>
</inset>
如果您这样做,可绘制对象将具有隐含的填充,并且您在设置小部件样式时不必设置它。由于按钮有多个状态可绘制对象,我建议您将它们全部转换为 XML 以确保线宽和角匹配。
尽管根元素的名称是 <shape>
实际上膨胀为 GradientDrawable
(这意味着您可以用渐变而不是纯色填充它)。查看 GradientDrawable
docs 的所有选项。不要曾经以编程方式使用ShapeDrawable
,它根本不起作用。
正在分析 9 个补丁
考虑以下三张放大图像。
候选人#1 是九补丁。它在每一侧都保留了 1px 用于拉伸和填充规范。这一张在拉伸时将表现为普通位图。如果宽度大于高度,则两侧的边框厚度也将大于高度。边框将按比例缩放。
Candidate #2 也是一个九补丁,实际上是说它会拉伸除 1px 边框之外的所有内容,并且每边都有 3px 的隐含填充。拉伸时它会有一个清晰的 1px 边框。
候选人 #3 不是 九补丁。它的缩放方式与#1 相同。
现在让我们看一下您在 OP 中包含的图片的放大版本:
如您所见,它不是一个九补丁,因此它不会被解释为一个补丁,并且构建工具足够友好地警告您这一点。即使较旧的构建工具更宽容并为您在每一侧添加透明 1px,结果也会表现得像普通位图,这意味着当拉伸时它看起来像样本 A 而不是预期结果样本 B。
这是more reading on nine-patches。这解释了每边额外的 1px 的用途。
给出的例子不是 9 块图像,正如它所说的那样。
如果您不想修改每个资源以将其转换为有效的 9 补丁资源,您可以尝试删除资源名称中的“.9”。这样,行为应该是相同的,并且不会出现构建错误。
要解决此问题,请确保用黑色或红色点填充所有边缘。这解决了我所有 SDK 版本的问题。如果您不想垂直或水平缩放或两者都缩放,只需填充所有边缘。在这种情况下,图像不会缩放。
Without error
With malformed error