WebP Compression

Cwebp

pngquant 不同,cwebpopen in new window 采用的是 BSD Licenseopen in new window,与 Apache License 并不冲突,所以 booster-task-compression-cwebpopen in new window 内置了从 Google 官方提供的下载地址:https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html 下载的 cwebp 二进制可执行文件 64 位版本。

How it Works?

PNG 图片压缩 的思路一样,也是在 mergeResprocessRes 任务之间插入 WebP 压缩任务,如下图所示:

由于 WebP 格式在 14 <= minSdkVersion <= 17 不支持带 alpha 通道的图像,所以,针对 AAPT2, alpha 两个维度,将 task 分成了4 种:

WARNING

需要注意的是:如果应用是发布在 Google Play 上,应用的启动图标必须是 PNG 格式open in new window,否则 Google Play 不会接受。

Getting Started

开启 WebP 压缩只需要引入 booster-task-compression-cwebpopen in new window 即可,如下所示:

buildscript {
    ext {
        kotlin_version = "1.3.31"
        booster_version = "4.11.0"
    }
    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
        maven { url 'https://oss.sonatype.org/content/repositories/public/' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.didiglobal.booster:booster-gradle-plugin:$booster_version"

        /* 👇👇👇👇 引用这个模块 👇👇👇👇 */
        classpath "com.didiglobal.booster:booster-task-compression-cwebp:$booster_version"
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

然后,执行 compress{Variant}ResourcesWithCwebp 任务来完成对资源的压缩,如下所示:

$ ./gradlew compressDebugResourcesWithCwebp --info
1

Combined with PNG Compression

由于只有 minSdkVersion18 以上才能完全使用 WebP 压缩,所以,为了解决 minSdkVerion18 以下,部分图片无法使用 WebP 压缩的情况,这时候就需要用 booster-task-compression-pngquantopen in new window 来弥补,当 booster-task-compression-pngquantopen in new windowbooster-task-compression-cwebpopen in new window 共存时,task 的依赖关系是这样的:

同时启用 PNGWebP 压缩只需要引入 booster-task-compression-cwebpopen in new windowbooster-task-compression-pngquantopen in new windowbooster-pngquant-provideropen in new window 即可:

buildscript {
    ext {
        kotlin_version = "1.3.31"
        booster_version = "4.11.0"
    }
    repositories {
        mavenLocal()
        mavenCentral()
        google()
        jcenter()
        maven { url 'https://oss.sonatype.org/content/repositories/public/' }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.didiglobal.booster:booster-gradle-plugin:$booster_version"

        /* 👇👇👇👇 引用这三个模块 👇👇👇👇 */
        classpath "com.didiglobal.booster:booster-task-compression-cwebp:$booster_version"
        classpath "com.didiglobal.booster:booster-task-compression-pngquant:$booster_version"
        classpath "io.johnsonlee.booster:booster-pngqaunt-provider:1.1.0"
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

WARNING

Android Gradle Plugin 3.6 及以上版本,需要在 gradle.properties 中设置:

android.precompileDependenciesResources=false
1

Custom Compression Quality

booster-task-compression-cwebpopen in new window 支持自定义的参数如下表所示:

选项描述
booster.task.compression.cwebp.option.quality压缩质量 (默认 80)

Configuring by gradle.properties

booster.task.compression.cwebp.option.quality=75
1

Configuring by Command Line

$ ./gradlew assembleDebug -Pbooster.task.compression.cwebp.option.quality=75
1

The Report

compress${Variant}ResourcesWithCwebp 任务执行完成后会自动在以下路径中生成任务报告:

build/reports/booster-task-compression-cwebp/${variant}/report.txt
1

其内容如下所示:

88.03% intermediates/res/merged/debug/drawable-xxhdpi_bg_banner2.png.flat       157,448 178,864 ...
88.03% intermediates/res/merged/debug/drawable-xxhdpi_bg_banner1.png.flat       157,448 178,864 ...
53.41% intermediates/res/merged/debug/drawable-xxxhdpi_bg_card.png.flat          49,315  92,325 ...
55.69% intermediates/res/merged/debug/drawable-xxxhdpi_bg_box.png.flat           19,387  34,813 ...
    0% intermediates/res/merged/debug/mipmap-xxxhdpi_ic_launcher_round.png.flat       0  15,308 ...
    0% intermediates/res/merged/debug/drawable-xxxhdpi_bg_chart.png.flat              0  10,653 ...
    0% intermediates/res/merged/debug/mipmap-xxhdpi_ic_launcher_round.png.flat        0  10,592 ...
    0% intermediates/res/merged/debug/mipmap-xxxhdpi_ic_launcher.png.flat             0   9,292 ...
    0% intermediates/res/merged/debug/mipmap-xhdpi_ic_launcher_round.png.flat         0   7,072 ...
    0% intermediates/res/merged/debug/mipmap-xxhdpi_ic_launcher.png.flat              0   6,552 ...
    0% intermediates/res/merged/debug/mipmap-hdpi_ic_launcher_round.png.flat          0   5,084 ...
    0% intermediates/res/merged/debug/mipmap-xhdpi_ic_launcher.png.flat               0   4,656 ...
    0% intermediates/res/merged/debug/mipmap-hdpi_ic_launcher.png.flat                0   3,128 ...
    0% intermediates/res/merged/debug/mipmap-mdpi_ic_launcher_round.png.flat          0   2,960 ...
    0% intermediates/res/merged/debug/mipmap-mdpi_ic_launcher.png.flat                0   2,224 ...
---------------------------------------------------------------------------------------
 TOTAL                                                                          383,598
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

其中,各列涵义如下所示:

  1. 压缩率 (压缩前的大小 - 压缩后的大小) / 压缩前的大小
  2. 合并后的资源文件路径
  3. 压缩后的大小
  4. 压缩前的大小
  5. 原始路径