Making PNG optimization part of your app’s diet
Henrique Boregio

Great post, i like the idea of having this as part of the build, the only thing is that our team works in both Windows and Mac OS, so we couldn’t use this .sh for our project, and thus I created an all Gradle version of your script, this works both on Windows and MacOs and it runs it trough all the modules in your project, i also removed the exiftool part because i didn’t want every developer that wanted to use this to have to install an extra tool, so i came up with the easy workaround of marking them as read only, anything that is mark as such will be omitted from the processing.

task optimizePngs(type: Exec) {
group = 'Optimizations'
description = 'Reduce the PNG size using PNGQuant'
commandLine 'echo', 'Optimizing Pngs (Do not remove me)'

def final excludedDirs = ['.git', 'build', '.externalNativeBuild', 'java']
def count = 0
project.subprojects.each {
module ->
type: FileType.DIRECTORIES,
preDir: {
if ( in excludedDirs) return FileVisitResult.SKIP_SUBTREE
excludeNameFilter: { it in excludedDirs },
nameFilter: { it.contains('drawable') }
) {
it.eachFile {
drawable ->
if (drawable.canWrite() &&'.png') && !'.9.png')) {
def pngQuantFlags = '--ext .png --force'
exec {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine "cmd", "/c", "..\\Location\\to\\program\\pngquant.exe $drawable.absolutePath $pngQuantFlags"
} else {
commandLine "../Location/to/program/pngquant $drawable.absolutePath $pngQuantFlags"

// Prints the number of pictures that were optimized
println '---------------------------------------------'
println " $count pictures were optimized "
println '---------------------------------------------'