WebP Compression
Cwebp
Unlike pngquant, cwebpopen in new window uses the BSD Licenseopen in new window, which does not conflict with the Apache License. Therefore, booster-task-compression-cwebpopen in new window has built-in 64-bit cwebp binary executables downloaded from the official Google download address: https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html.
How it Works?
Similar to PNG Image Compression, a WebP compression task is inserted between the mergeRes and processRes tasks, as shown below:
Since WebP format does not support images with alpha channel when 14 <= minSdkVersion <= 17, the task is divided into 4 types based on the two dimensions of AAPT2 and alpha:
WARNING
Note: If your app is published on Google Play, the app launcher icon must be in PNG formatopen in new window, otherwise Google Play will not accept it.
Getting Started
To enable WebP compression, simply include booster-task-compression-cwebpopen in new window, as shown below:
buildscript {
ext {
kotlin_version = "1.5.31"
booster_version = "4.16.3"
}
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"
/* 👇👇👇👇 Include this module 👇👇👇👇 */
classpath "com.didiglobal.booster:booster-task-compression-cwebp:$booster_version"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Then, execute the compress{Variant}ResourcesWithCwebp task to complete the resource compression, as shown below:
$ ./gradlew compressDebugResourcesWithCwebp --info
Combined with PNG Compression
Since only minSdkVersion 18 and above can fully use WebP compression, to address the situation where some images cannot be compressed with WebP when minSdkVersion is below 18, you need to use booster-task-compression-pngquantopen in new window as a complement. When booster-task-compression-pngquantopen in new window and booster-task-compression-cwebpopen in new window coexist, the task dependency relationship is as follows:
To enable both PNG and WebP compression, simply include booster-task-compression-cwebpopen in new window, booster-task-compression-pngquantopen in new window and booster-pngquant-provideropen in new window:
buildscript {
ext {
kotlin_version = "1.5.31"
booster_version = "4.16.3"
}
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"
/* 👇👇👇👇 Include these three modules 👇👇👇👇 */
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"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
WARNING
For Android Gradle Plugin 3.6 and above, you need to set in gradle.properties:
android.precompileDependenciesResources=false
WARNING
For Android Gradle Plugin 7.0 and above, you need to set in gradle.properties:
android.enableSourceSetPathsMap=false
Custom Compression Quality
booster-task-compression-cwebpopen in new window supports the following custom parameters:
| Option | Description |
|---|---|
booster.task.compression.cwebp.option.quality | Compression quality (default 80) |
Configuring by gradle.properties
booster.task.compression.cwebp.option.quality=75
Configuring by Command Line
$ ./gradlew assembleDebug -Pbooster.task.compression.cwebp.option.quality=75
The Report
When the compress${Variant}ResourcesWithCwebp task completes, a task report will be automatically generated at the following path:
build/reports/booster-task-compression-cwebp/${variant}/report.txt
The content is as shown below:
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
The meaning of each column is as follows:
- Compression ratio
(size before compression - size after compression) / size before compression - Merged resource file path
- Size after compression
- Size before compression
- Original path
