Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio / gradle: automated image resizing for android

I'm using Android Studio with Gradle.

I wonder whether there is any way to automate image resizing for different resolutions at build time. I mean to have one set of the images and to resize automatically to various resolutions (e.g. xxhdpi, xhdpi, hdpi, mdpi, ldpi)

Perfect solution would be to have some gradle plugin which can be configured to produce resized images in appropriate folder before building apk

like image 665
Denis Itskovich Avatar asked Mar 16 '14 16:03

Denis Itskovich


1 Answers

I developed a gradle plugin that allow doing exactly that (based on imagemagick)

First, you need to add the plugin to the build script :

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.eowise:gradle-imagemagick:0.4.0'
  }
}

Then, add a task for each size :

[
        [ variant: 'mdpi', size: '33%' ],
        [ variant: 'hdpi', size: '50%' ],
        [ variant: 'xhdpi', size: '66%' ],
        [ variant: 'xxhdpi', size: '100%' ]
].each {
    item ->
        task("buildIcons${item.variant.capitalize()}", type: com.eowise.imagemagick.tasks.Magick) {
            convert 'resources/images', { include '*.png' }
            into "app/src/main/res/drawable-${item.variant}"
            actions {
                -background('none')
                inputFile()
                -resize(item.size)
                outputFile()
            }
        }
}

You can find more informations about how to use this plugin on the wiki page on GitHub.

like image 119
Aurel Avatar answered Nov 15 '22 00:11

Aurel