Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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 foldernewFile('app\\src\\art\\drawable').eachFileMatch(~/.*\.svg/) { file ->
        // look for destination foldersnewFile('app\\src\\main\\res').eachFileMatch(~/drawable-.*/) { outputDir ->

            // define size based on folder name
            def size = ''switch (outputDir.getAbsolutePath()) {
                case ~/.*-ldpi/:
                    size = '36'breakcase ~/.*-mdpi/:
                    size = '48'breakcase ~/.*-hdpi/:
                    size = '72'breakcase ~/.*-xhdpi/:
                    size = '96'breakcase ~/.*-xxhdpi/:
                    size = '144'breakcase ~/.*-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:

Post a Comment for "External Dependencies On Gradle"