Android Gradle 插件自定义 Gradle 插件优化图片 ② ( 压缩 pngjpg 图片 | 使用 pngcrush 压缩工具压缩 png 图片 )
Posted 韩曙亮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Gradle 插件自定义 Gradle 插件优化图片 ② ( 压缩 pngjpg 图片 | 使用 pngcrush 压缩工具压缩 png 图片 )相关的知识,希望对你有一定的参考价值。
文章目录
Android Plugin DSL Reference 参考文档 :
-
Android Studio 构建配置官方文档 : https://developer.android.google.cn/studio/build
-
添加构建依赖项 参考文档 : https://developer.android.google.cn/studio/build/dependencies
-
Android Gradle 插件配置与 Gradle 配置关联 : 【Android Gradle 插件】Gradle 依赖管理 ① ( org.gradle.api.Project 配置 | Android Gradle 插件配置与 Gradle 配置关联 ) ★
-
Android Gradle 插件文档主页 : https://google.github.io/android-gradle-dsl/2.3/
-
BaseExtension 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.BaseExtension.html
-
AppExtension ( build.gradle#android 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.AppExtension.html
-
build.gradle#android 模块配置文档 : android-gradle-dsl/2.3/com.android.build.gradle.AppExtension.html
-
ProductFlavor ( build.gradle#android#defaultConfig 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
-
ProductFlavor#externalNativeBuild 配置 ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) : com.android.build.gradle.internal.dsl.ProductFlavor:externalNativeBuild
-
ExternalNativeBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ExternalNativeBuildOptions.html
-
NdkBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild#ndkBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.NdkBuildOptions.html
-
CmakeOptions ( build.gradle#android#defaultConfig#externalNativeBuild#cmake 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.CmakeOptions.html
-
JackOptions ( build.gradle#android#defaultConfig#jackOptions 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.JackOptions.html
-
AnnotationProcessorOptions ( 注解处理器配置 ) ( build.gradle#android#defaultConfig#javaCompileOptions配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AnnotationProcessorOptions.html
-
NdkOptions ( build.gradle#android#defaultConfig#ndk 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.NdkOptions.html
-
NDK 参考文档 : Add C and C++ Code to Your Project.
-
VectorDrawablesOptions ( build.gradle#android#defaultConfig#vectorDrawables 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html
-
BuildType ( build.gradle#android#buildTypes 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.BuildType.html
-
SigningConfig ( build.gradle#android#signingConfigs 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.SigningConfig.html
-
AaptOptions ( build.gradle#android#aaptOptions 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AaptOptions.html
-
AndroidSourceSets ( build.gradle#android#sourceSets 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceSet.html
-
AndroidSourceDirectorySet ( build.gradle#android#sourceSets#aidl/assets/java/jni/jniLibs 配置 ) 文档位置 :android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceDirectorySet.html
-
AndroidSourceFile ( build.gradle#android#sourceSets#manifest 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.api.AndroidSourceFile.html
-
DataBindingOptions ( build.gradle#android#dataBinding ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DataBindingOptions.html
-
DexOptions ( build.gradle#android#dexOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DexOptions.html
-
LintOptions ( build.gradle#android#lintOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.LintOptions.html
-
PackagingOptions ( build.gradle#android#packagingOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.PackagingOptions.html
-
Splits ( build.gradle#android#splits ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.Splits.html
-
AbiSplitOptions ( build.gradle#android#splits#abi ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AbiSplitOptions.html
-
LanguageSplitOptions ( build.gradle#android#splits#language ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.LanguageSplitOptions.html
-
DensitySplitOptions ( build.gradle#android#splits#density ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.DensitySplitOptions.html
-
TestOptions ( build.gradle#android#testOptions ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.html
-
Android 测试相关文档 : https://developer.android.google.cn/training/testing
-
UnitTestOptions ( build.gradle#android#testOptions#unitTests ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.TestOptions.UnitTestOptions.html
-
org.gradle.api.tasks.testing.Test 单元测试配置类 : https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html
-
LibraryExtension ( Library 依赖库的 build.gradle#android 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.LibraryExtension.html
-
org.gradle.api.Project 配置 ( build.gradle 根配置 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html
-
org.gradle.api.initialization.Settings 配置 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/initialization/Settings.html
-
org.gradle.api.artifacts.dsl.DependencyHandler 配置 ( build.gradle#dependencies#implementation 配置 ) : https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html
-
添加构建依赖项 参考文档 : https://developer.android.google.cn/studio/build/dependencies
-
org.gradle.api.Task 配置 ( Gradle 任务类型 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html
-
org.gradle.api.tasks.TaskContainer配置 ( Gradle 任务容器类型 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskContainer.html
-
org.gradle.api.DefaultTask 配置 ( Gradle 自定义任务类父类 ) 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/DefaultTask.html
-
org.gradle.api.tasks.Delete 自带任务 ( clean 任务类型 ) 文档 : https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Delete.html
-
org.gradle.api.tasks.bundling.Zip 自带任务 ( 任务类型 ) 文档 :https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html
-
org.gradle.api.tasks.bundling.Jar自带任务 ( 任务类型 ) 文档 : https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html
-
Gradle 自带任务查询界面 ( 搜索 Task Types ) : https://docs.gradle.org/current/dsl/#N1045C
-
org.gradle.api.invocation.Gradle 文档 ( 可设置 Gradle 构建声明周期监听器 ) : https://docs.gradle.org/current/javadoc/org/gradle/api/invocation/Gradle.html
-
org.gradle.BuildListener 构建监听器 文档 : https://docs.gradle.org/current/javadoc/org/gradle/BuildListener.html
-
org.gradle.execution.TaskExecutionGraphListener 构建监听器 文档 : https://docs.gradle.org/current/javadoc/org/gradle/api/execution/TaskExecutionGraphListener.html
-
自定义 Gradle 插件 - GitHub 地址 : https://github.com/han1202012/Android_UI
一、压缩 png、jpg 图片
在 android 应用中 , 最常见的图片格式是 png 和 jpg 格式 , 为这两种图片格式选择合适的图片压缩工具 :
- png 图片 : pngcrush 压缩工具 , 该工具时无损压缩工具 , 官网 https://pmt.sourceforge.io/pngcrush/ ;
- jpg 图片 : guetzli 压缩工具 ;
二、使用 pngcrush 压缩工具压缩 png 图片
pngcrush 官网 : https://pmt.sourceforge.io/pngcrush/
点击左上角的 按钮 , 即可进入下载页 ;
pngcrush 下载页 : https://sourceforge.net/projects/pmt/files/
下图中 , pngcrush 中是源码 , pngcrush-executables 是各个平台的可执行文件 , 这里我们选择下载 Windows 系统中的可执行文件 ;
进入可执行文件下载页面 https://sourceforge.net/projects/pmt/files/pngcrush-executables/ , 选择下载最新版本的软件 ;
在 https://sourceforge.net/projects/pmt/files/pngcrush-executables/1.8.11/ 页面中, 下载 Windows 系统中 64 位的可执行文件 ;
下载后 , 在 cmd 命令行中执行该程序 , 会提示使用方法 ;
D:\\001_Develop\\045_pngcrush>pngcrush_1_8_11_w64.exe
| pngcrush-1.8.11
| Copyright (C) 1998-2002, 2006-2016 Glenn Randers-Pehrson
| Portions Copyright (C) 2005 Greg Roelofs
| This is a free, open-source program. Permission is irrevocably
| granted to everyone to use this version of pngcrush without
| payment of any fee.
| Executable name is pngcrush_1_8_11_w64.exe
| It was built with bundled libpng-1.6.28
| and is running with bundled libpng-1.6.28
| Copyright (C) 1998-2004, 2006-2016 Glenn Randers-Pehrson,
| Copyright (C) 1996, 1997 Andreas Dilger,
| Copyright (C) 1995, Guy Eric Schalnat, Group 42 Inc.,
| and bundled zlib-1.2.11, Copyright (C) 1995-2017,
| Jean-loup Gailly and Mark Adler,
| and using "clock()".
| It was compiled with gcc version 4.8.0 20121031 (experimental).
If you have modified this source, you may insert additional notices
immediately after this sentence.
Copyright (C) 1998-2002, 2006-2016 Glenn Randers-Pehrson
Portions Copyright (C) 2005 Greg Roelofs
DISCLAIMER: The pngcrush computer program is supplied "AS IS".
The Author disclaims all warranties, expressed or implied, including,
without limitation, the warranties of merchantability and of fitness
for any purpose. The Author assumes no liability for direct, indirect,
incidental, special, exemplary, or consequential damages, which may
result from the use of the computer program, even if advised of the
possibility of such damage. There is no warranty against interference
with your enjoyment of the computer program or against infringement.
There is no warranty that my efforts or the computer program will
fulfill any of your particular purposes or needs. This computer
program is provided with all faults, and the entire risk of satisfactory
quality, performance, accuracy, and effort is with the user.
LICENSE: Permission is hereby irrevocably granted to everyone to use,
copy, modify, and distribute this computer program, or portions hereof,
purpose, without payment of any fee, subject to the following
restrictions:
1. The origin of this binary or source code must not be misrepresented.
2. Altered versions must be plainly marked as such and must not be
misrepresented as being the original binary or source.
3. The Copyright notice, disclaimer, and license may not be removed
or altered from any source, binary, or altered source distribution.
usage: pngcrush_1_8_11_w64.exe [options except for -e -d] infile.png outfile.png
pngcrush_1_8_11_w64.exe -e ext [other options] file.png ...
pngcrush_1_8_11_w64.exe -d dir/ [other options] file.png ...
pngcrush_1_8_11_w64.exe -ow [other options] file.png [tempfile.png]
pngcrush_1_8_11_w64.exe -n -v file.png ...
options:
-bail (bail out of trial when size exceeds best size found
-blacken (zero samples underlying fully-transparent pixels)
-brute (use brute-force: try 148 different methods)
-c color_type of output file [0, 2, 4, or 6]
-check (check CRC and ADLER32 checksums)
-d directory_name/ (where output files will go)
-e extension (used for creating output filename)
-f user_filter [0-5] for specified method
-fix (salvage PNG with otherwise fatal conditions)
-force (write output even if IDAT is larger)
-g gamma (float or fixed*100000, e.g., 0.45455 or 45455)
-huffman (use only zlib strategy 2, Huffman-only)
-iccp length "Profile Name" iccp_file
-itxt b[efore_IDAT]|a[fter_IDAT] "keyword"
-keep chunk_name
-l zlib_compression_level [0-9] for specified method
-loco ("loco crush" truecolor PNGs)
-m method [1 through 150]
-max maximum_IDAT_size [default 524288L]
-mng (write a new MNG, do not crush embedded PNGs)
-n (no save; doesn't do compression or write output PNG)
-new (Use new default settings (-reduce))
-newtimestamp (Reset file modification time [default])
-nobail (do not bail out early from trial -- see "-bail")
-nocheck (do not check CRC and ADLER32 checksums)
-nofilecheck (do not check for infile.png == outfile.png)
-noforce (default; do not write output when IDAT is larger)
-nolimits (turns off limits on width, height, cache, malloc)
-noreduce (turns off all "-reduce" operations)
-noreduce_palette (turns off "-reduce_palette" operation)
-old (Use old default settings (no -reduce))
-oldtimestamp (Do not reset file modification time)
-ow (Overwrite)
-q (quiet) suppresses console output except for warnings
-reduce (do lossless color-type or bit-depth reduction)
-rem chunkname (or "alla" or "allb")
-replace_gamma gamma (float or fixed*100000) even if it is present.
-res resolution in dpi
-rle (use only zlib strategy 3, RLE-only)
-s (silent) suppresses console output including warnings
-save (keep all copy-unsafe PNG chunks)
-speed Avoid the AVG and PAETH filters, for decoding speed
-srgb [0, 1, 2, or 3]
-ster [0 or 1]
-text b[efore_IDAT]|a[fter_IDAT] "keyword" "text"
-trns_array n trns[0] trns[1] .. trns[n-1]
-trns index red green blue gray
-v (display more detailed information)
-version (display the pngcrush version)
-warn (only show warnings)
-w compression_window_size [32, 16, 8, 4, 2, 1, 512]
-z zlib_strategy [0, 1, 2, or 3] for specified method
-zmem zlib_compression_mem_level [1-9, default 9]
-zitxt b|a "keyword" "lcode" "tkey" "text"
-ztxt b[efore_IDAT]|a[fter_IDAT] "keyword" "text"
-h (help and legal notices)
-p (pause)
D:\\001_Develop\\045_pngcrush>
使用 执行
pngcrush_1_8_11_w64.exe -brute -rem alla -reduce input.png output.png
命令 , 进行 png 图片压缩 ;
-brute
参数的作用是设置 " 从 148 种不同的方法中选择最合适的方案进行压缩 " ;-rem alla
参数作用是设置 " 移除 png 格式图片中无用的模块 " ;-reduce
参数作用是设置 " 排除无用的颜色 " ;
在命令最后有两个文件路径 , 前者是输入文件 , 后者是输出文件 ;
命令行输入如下 :
D:\\001_Develop\\045_pngcrush>pngcrush_1_8_11_w64.exe -brute -rem alla -reduce input.png output.png
Recompressing IDAT chunks in input.png to output.png
Total length of data found in critical chunks = 4926
Critical chunk length, method 1 (ws 15 fm 0 zl 4 zs 0) = 5151
Critical chunk length, method 2 (ws 15 fm 1 zl 4 zs 0) > 5151
Critical chunk length, method 3 (ws 15 fm 5 zl 4 zs 1) = 5137
Critical chunk length, method 4 (ws 15 fm 0 zl 9 zs 1) > 5137
Critical chunk length, method 5 (ws 15 fm 1 zl 9 zs 1) = 5062
Critical chunk length, method 6 (ws 15 fm 5 zl 9 zs 0) = 4717
Critical chunk length, method 7 (ws 15 fm 0 zl 9 zs 0) > 4717
Critical chunk length, method 8 (ws 15 fm 1 zl 9 zs 1) > 4717
Critical chunk length, method 9 (ws 15 fm 5 zl 2 zs 2) > 4717
Critical chunk length, method 10 (ws 15 fm 5 zl 9 zs 1) = 4654
Critical chunk length, method 11 (ws 15 fm 0 zl 2 zs 2) > 4654
Critical chunk length, method 148 (ws 15 fm 5 zl 0 zs 1) > 4654
Best pngcrush method = 10 (ws 15 fm 5 zl 9 zs 1) = 4654
(5.52% critical chunk reduction)
(5.64% filesize reduction)
CPU time decode 0.011640, encode 4.277780, other 4.296384, total 4.291837 sec
压缩结果 : 源图 input.png 4.81KB , 转换后的图片 output.png 4.53KB ;
以上是关于Android Gradle 插件自定义 Gradle 插件优化图片 ② ( 压缩 pngjpg 图片 | 使用 pngcrush 压缩工具压缩 png 图片 )的主要内容,如果未能解决你的问题,请参考以下文章
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑤ ( 自定义插件中获定义方法 | 在插件中创建 Gradle 任务 | 代码示例 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑦ ( 自定义 Gradle 插件导入方式 | buildSrc 插件导入 | 构建脚本中自定义插件 | 独立文件 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ⑦ ( 自定义 Gradle 插件导入方式 | buildSrc 插件导入 | 构建脚本中自定义插件 | 独立文件 )
Android Gradle 插件自定义 Gradle 插件模块 ② ( 在模块中定义插件 | 引入自定义 Gradle 插件模块 | 配置 Gradle 插件上传选项 | 配置分组名称版本号 )
Android Gradle 插件Gradle 自定义 Plugin 插件 ④ ( 为自定义 Gradle 插件的扩展配置扩展 | 在自定义插件中获取扩展属性 )