文章目录
- 一、报错信息
- 二、解决方案
一、报错信息
编译 Android Studio 项目时 , 报错
代码语言:javascript复制AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"D:\002_Project\002_Android_Learn\ClassLoader_Demo\app\src\main\res\drawable\abc.png"}],"original":"ERROR:D:\002_Project\002_Android_Learn\ClassLoader_Demo\app\src\main\res\drawable\abc.png: AAPT: error: failed to read PNG signature: file does not start with PNG signature.n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"D:\002_Project\002_Android_Learn\ClassLoader_Demo\app\src\main\res\drawable\abc.png"}],"original":"ERROR:D:\002_Project\002_Android_Learn\ClassLoader_Demo\app\src\main\res\drawable\abc.png: AAPT: error: file failed to compile.n ","tool":"AAPT"}
> Task :app:mergeDebugResources FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.res.Aapt2CompileRunnable
> Android resource compilation failed
ERROR:D: 02_Project 02_Android_LearnClassLoader_Demoappsrcmainresdrawableabc.png: AAPT: error: failed to read PNG signature: file does not start with PNG signature.
ERROR:D: 02_Project 02_Android_LearnClassLoader_Demoappsrcmainresdrawableabc.png: AAPT: error: file failed to compile.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
57 actionable tasks: 3 executed, 54 up-to-date
二、解决方案
报错的原因是 Android 项目中使用的 png 格式的图片格式错误 ,
在 build.gradle 中的 android 层级配置了
代码语言:javascript复制 aaptOptions {
// aapt 附加参数
additionalParameters '--rename-manifest-package', 'com.example.classloader_demo2'
// 是否开启 png 图片优化检查
cruncherEnabled true
}
配置 , cruncherEnabled 配置的作用是开启 png 图片优化检查 ,
如果 png 图片格式错误 , 就会报上述错误 ;
解决方案 :
① 删除上述报错的 abc.png 图片即可 ;
② 如果想要保留上述图片 , 则将 " android # aaptOptions # cruncherEnabled " 设置为 false 即可 , 程序可编译完成 ;
代码语言:javascript复制 aaptOptions {
// aapt 附加参数
additionalParameters '--rename-manifest-package', 'com.example.classloader_demo2'
// 是否开启 png 图片优化检查
cruncherEnabled true
}