2

I have a build.gradle for my Android app and then some helper.gradle files for various sub tasks. Some of these helper tasks invoke shell scripts. Because this app has been in development on exclusively Linux and Mac machines, the shell scripts run fine.

I've recently added a Windows development machine into the mix and the gradle build configuration is failing at calling the shell script.

task helperTask1 << {
    exec {
        workingDir rootProject.getProjectDir()
        commandLine 'sh', './scripts/helperScript1.sh'
    }
}

At some point I'll get Cygwin up and running on the Windows machine, but what are my options for getting my app compiling?

  1. Is there a way to effectively have a helperTask1_Linux and helperTask1_Windows tasks that get called on their respective platforms?
  2. Is there a more "gradle" way of calling shell scripts so that my gradle files are defining steps at a higher level?
FishStix
  • 4,994
  • 9
  • 38
  • 53
  • 1
    1. Try [detecting the operating system](http://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java) and branching accordingly. I don't know how well that will work, but it's worth a try. 2. We don't know what the scripts are for and we don't know what "a higher level" is with respect to your app, so that will be difficult for anyone to answer. – CommonsWare Jan 21 '17 at 15:49
  • Gradle strives to be more declarative - I'm wondering if there is a sort of 'best practices' way to think about adding helper tasks to a gradle build process. Directly calling shell scripts feels more hacky and sure enough - gradle is no longer platform agnostic. – FishStix Jan 21 '17 at 16:18

1 Answers1

1

Here is how I'm doing this:

  1. define a property 'ant.condition.os'. The value of the property depends of the OS.
  2. use the value that property in a switch to call a windows or linux script.

ant.condition(property: "os", value: "windows") { os(family: "windows") }
ant.condition(property: "os", value: "unix"   ) { os(family: "unix")    }

task myTask() {
    switch (ant.properties.os) {
        case 'windows':
            commandLine 'cmd', '/c', 'myscript.cmd'
            break;
        case 'unix':
            commandLine './myscript.sh'
            break;
    }
}
ben75
  • 29,217
  • 10
  • 88
  • 134