Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解決 Android Gradle 報錯「Some file crunching failed」:關閉圖片壓縮檢查

編輯:關於 Android 開發

錯誤現象

打包時遇到類似這樣的報錯:

Error: java.lang.RuntimeException: Some file crunching failed, see logs for details

而完整日誌通常更明確:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> Error: java.lang.RuntimeException: Crunching Cruncher ic_default_home_new.png failed, see logs

* Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.

根本原因

Gradle 在合併資源階段會對 PNG 圖片執行 crunching(再壓縮)處理,同時會檢查文件後綴名與真實格式是否一致。最常見的觸發原因就是把 jpg 圖片直接改名成 png——文件內容仍是 JPEG,擴展名卻寫著 .png,cruncher 解析失敗,於是整個構建中斷。

另一種可能是圖片本身已損壞或使用了 cruncher 不支援的 PNG 變體。

解決方案一:關閉 cruncher

在模塊的 build.gradle 的 android 節點中加入 aaptOptions 配置:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    aaptOptions {
        cruncherEnabled = false
        useNewCruncher = false
    }

    defaultConfig {
        applicationId "com.security.browser.xinj"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 2
        versionName "1.0.1"
        ndk {
            // 設置支持的 SO 庫架構
            abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }
    }
}

加上這段配置後重新 Clean 並 Rebuild,錯誤即可消除。

更值得推薦的做法

關閉 cruncher 只是繞過檢查,問題根源仍在。更穩妥的處理方式是:
1、用圖片工具重新打開並另存為標準 PNG,確保格式與擴展名一致;
2、檢查是否存在同名不同格式的殘留文件,反覆清理資源目錄;
3、確認 buildToolsVersion 與 compileSdkVersion 的組合沒有已知的 aapt 缺陷。

如果項目對包體積敏感,關閉 crunching 也意味著放棄了一部分 PNG 再壓縮收益,權衡之後再決定更合適。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved