Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External dependencies on Gradle

I'm familiar with building large applications using make, but now I have begun using Android Studio and I want to understand how to do things I already do in a Makefile.

Here are a example that might help you frame an answer:

Makefile example: (minimalist)

INK=inkscape
INKFLAGS=--export-area-page

# Resolve *.png dependencies
drawable-mdpi/%.png: %.svg
         $(INK) $< --export-png=$@ -w 48 -h 48 $(INKFLAGS) 

drawable-hdpi/%.png: %.svg
         $(INK) $< --export-png=$@ -w 72 -h 72 $(INKFLAGS) 

drawable-xdpi/%.png: %.svg
         $(INK) $< --export-png=$@ -w 96 -h 96 $(INKFLAGS) 

More simple example:

drawable-mdpi/ic_launcher.png: ic_launcher.svg
         inkscape ic_launcher.svg --export-png=ic_launcher.png -w 48 -h 48 --export-area-page 

drawable-hdpi/ic_launcher.png: ic_launcher.svg
         inkscape ic_launcher.svg --export-png=ic_launcher.png -w 72 -h 72 --export-area-page 

How to do that in Gradle?

I want to resolve external dependencies such as mentioned in the above example. Actually I'm doing it via 'make', but i want to completely remove this extra step.

like image 862
rnrneverdies Avatar asked Dec 18 '25 05:12

rnrneverdies


1 Answers

It is possible to run external commands from Grandle and integrate those into your build process. My example runs inkscape.exe on Windows and defines its parameters in the build script, you can also just run a shell script this way.

The following code goes into the app\build.gradle file. Task convertDrawable is written in Groovy syntax and accomplishes the following (tl;dr it is an implementation of your "simple example"):

  • It looks through all the *.svg files in a custom folder art/drawable
  • Within each of those *.svg files, looks through all the drawable-* folders in your resources folder
  • Based on drawable-* folder name, determine the target resolution.
  • Then calls inkscape.exe to convert each *.svg to *.png with the required size.

Code:

task convertDrawables() {
    def ink =  'C:\\Program Files (x86)\\Inkscape\\inkscape.exe'

    // look for *.svg files in app/src/art/drawable folder
    new File('app\\src\\art\\drawable').eachFileMatch(~/.*\.svg/) { file ->
        // look for destination folders
        new File('app\\src\\main\\res').eachFileMatch(~/drawable-.*/) { outputDir ->

            // define size based on folder name
            def size = ''
            switch (outputDir.getAbsolutePath()) {
                case ~/.*-ldpi/:
                    size = '36'
                    break
                case ~/.*-mdpi/:
                    size = '48'
                    break
                case ~/.*-hdpi/:
                    size = '72'
                    break
                case ~/.*-xhdpi/:
                    size = '96'
                    break
                case ~/.*-xxhdpi/:
                    size = '144'
                    break
                case ~/.*-xxxhdpi/:
                    size = '192'
                    break
            }
            def cmd = ink + ' ' + file.getCanonicalPath() + ' --export-png=' + outputDir.getAbsolutePath() + '\\ic_launcher2.png -w ' + size + ' -h ' + size + ' --export-area-page'
            def process = cmd.execute();
            process.waitFor();
        }
    }
}


// make sure the convertDrawable task is executed somewhere in the make process
gradle.projectsEvaluated {
    preBuild.dependsOn(convertDrawable)
}

Here are the resources I used:

  • Gradle Doc Chapter 15: More about Tasks
  • Gradle Doc Chapter 58. Writing Custom Task Classes
  • Gradle Doc Exec function
  • Groovy execute()
  • Stackoverflow Run task before compilation using Android Gradle plugin
  • Groovy Doc for File
like image 86
chrki Avatar answered Dec 19 '25 19:12

chrki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!