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
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"):
*.svg
files in a custom folder art/drawable
*.svg
files, looks through all the drawable-*
folders in your resources folderdrawable-*
folder name, determine the target resolution.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: