External dependencies on Gradle

前端 未结 1 857
面向向阳花
面向向阳花 2021-01-20 02:52

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

相关标签:
1条回答
  • 2021-01-20 03:21

    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
    0 讨论(0)
提交回复
热议问题